如何使用PHP从数据库/文件夹下载文件 [英] How to download file from database/folder using php

查看:205
本文介绍了如何使用PHP从数据库/文件夹下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上载的文件将存储在数据库和文件夹中(文件夹名称:uploads).现在我想知道如何从数据库/文件夹创建下载文件.我有两个文件butangDonload.php和download.php.当用户单击单词"donload"时,将出现弹出框并保存文件.

File uploaded will be store in database and folder (folder name: uploads).Now I want to know how to create download file from database/folder. I have two file butangDonload.php and download.php. When user click word "donload" the pop up box will appear and save the file.

butangDonload.php

<?php

    $file = "Bang.png"; //Let say If I put the file name Bang.png
    echo "<a href='download.php?nama=".$file."'>donload</a> ";

?>

download.php

<?php
    $name= $_GET['nama'];
    download($name);

    function download($name) {
        $file = $nama_fail;

        if (file_exists($file)) {
            header('Content-Description: File Transfer');
            header('Content-Type: application/octet-stream');
            header('Content-Disposition: attachment; filename='.basename($file));
            header('Content-Transfer-Encoding: binary');
            header('Expires: 0');
            header('Cache-Control: must-revalidate');
            header('Pragma: public');
            header('Content-Length: ' . filesize($file));
            ob_clean();
            flush();
            readfile($file);
            exit;
        }
    }
?>

推荐答案

我已更改为您的代码,只需稍作修改即可正常工作. 这是代码:

I have changed to your code with little modification will works well. Here is the code:

butangDonload.php

butangDonload.php

<?php
$file = "logo_ldg.png"; //Let say If I put the file name Bang.png
echo "<a href='download1.php?nama=".$file."'>download</a> ";
?>

download.php

download.php

<?php
$name= $_GET['nama'];

    header('Content-Description: File Transfer');
    header('Content-Type: application/force-download');
    header("Content-Disposition: attachment; filename=\"" . basename($name) . "\";");
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    header('Content-Length: ' . filesize($name));
    ob_clean();
    flush();
    readfile("your_file_path/".$name); //showing the path to the server where the file is to be download
    exit;
?>

在这里,您需要显示要下载文件的路径. 即将仅提供文件名,但需要提供用于读取该文件的文件路径.因此,应将其替换为 我已经通过使用您的代码进行了测试,修改也可以.

Here you need to show the path from where the file to be download. i.e. will just give the file name but need to give the file path for reading that file. So, it should be replaced by I have tested by using your code and modifying also will works.

这篇关于如何使用PHP从数据库/文件夹下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆