上传的文件将存储在数据库和文件夹中(文件夹名称:上传)。现在我想知道如何从数据库/文件夹创建下载文件。我有两个文件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> ";
?>
的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;
}
}
?>
答案 0 :(得分:13)
我已经更改为您的代码,只需稍加修改即可。 这是代码:
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
<?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;
?>
在这里,您需要显示要下载文件的路径。 即只提供文件名,但需要提供用于读取该文件的文件路径。所以,它应该被替换为 我已经通过使用您的代码进行了测试,修改也将有效。
答案 1 :(得分:3)
这里是下载文件的代码,下载了多少%
<?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>";
}
}
?>
答案 2 :(得分:2)
您可以使用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
答案 3 :(得分: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;
?>
答案 4 :(得分: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();
?>
答案 5 :(得分: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);
试试这个方法,对我很有帮助。