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

标签 php download

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

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

最佳答案

我已经更改了您的代码,稍加修改就可以正常工作。 这是代码:

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

这里需要显示下载文件的路径。 即只给出文件名,但需要给出读取该文件的文件路径。所以,它应该被替换为 我已经使用您的代码进行了测试,修改后也可以。

关于php - 如何使用php从数据库/文件夹下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20367104/

相关文章:

php - 如何使用 $_get VARIABLE IN WHERE 更新 Php Mysql 的表

php - PHP 的 SOAP 库

php - Sonata sonata_type_model_list 显示对象名称

php - CodeIgniter:帮助解决此查询

android - 显示 volley 文件下载的进度值

jsf - 在应用服务器上限时存储 PDF 并提供下载

http - IIS 文件下载挂起/超时 - sc-win32-status = 64

php - 仅通过 cron 运行 php 脚本

powershell-2.0 - 在 Windows 7 上使用 powershell 2.0 下载文件

audio - Windows CMD提示Youtube-Dl。无法将多个youtube mp3播放列表下载到特定的文件夹中