上传的文件将存储在数据库和文件夹中(文件夹名称: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/