如何使用PHP从数据库/文件夹下载文件

11

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

butangDownload.php

<?php

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

?>

下载.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;
        }
    }
?>

2
$nama_fail 是从哪里来的?为什么您从不使用传递给下载函数的 $name 参数的值? - Quentin
我已经将它更改为 $name。 - nazerol
6个回答

17

我稍微修改了你的代码,现在它可以正常工作了。以下是代码:

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> ";
?>

下载.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;
?>

在这里您需要展示文件下载的路径。 即只给出文件名是不够的,还需要给出读取该文件的文件路径。因此,应将其替换为 我已经通过使用您的代码并进行修改进行了测试,它也可以工作。


您还可以在以下链接中查看工作演示:http://websamplenow.com/29/file_download/ - Suman Immortal Maharzan
您可以通过此链接直接下载 <a href='download1.php?nama="asasa"' download>下载</a>,只需在锚标签中添加 download 属性即可。 - Vineet

4
以下是下载文件并显示下载百分比的代码:
<?php
$ch = curl_init();
$downloadFile = fopen( 'file name here', 'w' );
curl_setopt($ch, CURLOPT_URL, "file link here");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_BUFFERSIZE, 65536);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_PROGRESSFUNCTION, 'downloadProgress'); 
curl_setopt($ch, CURLOPT_NOPROGRESS, false);
curl_setopt( $ch, CURLOPT_FILE, $downloadFile );
curl_exec($ch);
curl_close($ch);

function downloadProgress ($resource, $download_size, $downloaded_size, $upload_size, $uploaded_size) {

    if($download_size!=0){
    $percen= (($downloaded_size/$download_size)*100);
    echo $percen."<br>";

}

}
?>

1
不是真正回答了初始问题,但这回答了我的问题 :) - Anton Kolenkov

3
您可以使用HTML5标签直接下载图片。
<?php
$file = "Bang.png"; //Let say If I put the file name Bang.png
echo "<a href='download.php?nama=".$file."' download>donload</a> ";
?>

更多信息,请查看此链接 http://www.w3schools.com/tags/att_a_download.asp


0
function downloadFiles($dir, $file) {
    header("Content-type: application/force-download");
    header('Content-Disposition: inline; filename="' . $dir . $file . '"');
    header("Content-Transfer-Encoding: Binary");
    header("Content-length: " . filesize($dir . $file));
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename="' . $file . '"');
    readfile("$dir$file");
}

$dir = "folder_path";

$file = "image_name.jpg";
$download = downloadFiles($dir,$file);

试试这种方式,对我很有帮助。


0
You can also use the following code:

<?php
$filename = $_GET["nama"];
$contenttype = "application/force-download";
header("Content-Type: " . $contenttype);
header("Content-Disposition: attachment; filename=\"" . basename($filename) . "\";");
readfile("your file uploaded path".$filename);
exit();
?>

2
这会让你可以访问服务器上的每个文件!这是一个巨大的安全漏洞。而且 application/force-download 不是一个注册的 MIME 类型。 - Quentin

0
butangDonload.php

$file = "Bang.png"; //Let say If I put the file name Bang.png
$_SESSION['name']=$file;    

试试这个,

<?php

$name=$_SESSION['name'];
download($name);

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

尽量避免只给出代码的答案。解释其背后的原理和工作方式会更有帮助。感谢您的发帖并继续努力。 - Joshua Wilson
改用会话而不是查询字符串并不能帮助解决问题,反而可能会导致数据被覆盖,特别是在多个标签页中浏览网站时。 - Quentin

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接