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

如何使用php从数据库/文件夹下载文件,php,download,Php,Download,上传的文件将存储在数据库和文件夹中(文件夹名称:uploads)。现在我想知道如何从数据库/文件夹创建下载文件。我有两个文件:butangDonload.php和download.php。当用户单击单词“donload”时,弹出框将出现并保存文件 butangDonload.php <?php $file = "Bang.png"; //Let say If I put the file name Bang.png echo "<a href='download.p

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

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

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

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

试试这个

<?php

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

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

我对您的代码做了一些修改,但效果良好。 代码如下:

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

这里需要显示要下载文件的路径。 i、 e.将只给出文件名,但需要给出读取该文件的文件路径。因此,它应该被替换为 我已经通过使用您的代码进行了测试,修改也将起作用。

您还可以使用以下代码:
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();
?>

您可以使用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> ";
?>

有关更多信息,请查看此链接

以下是下载文件的代码,其中包含下载的百分比

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

}

}
?>


试着这样做,对我很有帮助。

nama_fail是从哪里来的?为什么从不使用传递给下载函数的
$name
参数的值?我已经将其更改为$name。请尽量避免只使用代码的答案。解释发生了什么以及它是如何工作的更有帮助。感谢您的发布并继续努力。切换到会话而不是查询字符串不会有任何帮助,如果在多个选项卡中浏览网站,可能会导致数据被覆盖。您还可以在链接中查看工作演示:您可以通过此
直接下载,只需添加
下载
属性即可在锚定标记中,这允许访问服务器上的每个文件!这是一个巨大的安全漏洞。而且,
application/force download
不是注册的MIME类型。并不是真正回答最初的问题,但这回答了我的问题:)