php 如何在PHP中制作下载链接?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1218277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to make a download link in PHP?
提问by Amber
i can upload a file in the database using sql but how can i make a download link for it? like when you download something online then a message box will come up you will be asked if you would like to open it with a program or save it. how can i do that in php? can you give me the codes for it? i'm still a noob.
我可以使用 sql 上传数据库中的文件,但如何为其制作下载链接?例如,当您在线下载某些内容时,会出现一个消息框,系统会询问您是要使用程序打开它还是保存它。我怎么能在 php 中做到这一点?你能给我代码吗?我还是个菜鸟。
回答by Amber
Place this code on a page (along with the PHP code to get the info from the DB and place it in the variables for the name/size/data, then link to that page.
将此代码放在页面上(与 PHP 代码一起从数据库获取信息并将其放置在名称/大小/数据的变量中,然后链接到该页面。
<?php
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename=' . $name_of_file);
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . $size_of_file);
echo $file_data;
?>
Not all of the headers listed above are strictly necessary - in fact, only the Content-Type header is trulynecessary to make the download work properly. The Content-Disposition header is good to include so that you can specify a proper filename; the others just help the browser handled the download better and can be omitted if you desire.
并非上面列出的所有标头都是绝对必要的——事实上,只有 Content-Type 标头才是使下载正常工作真正必要的。包含 Content-Disposition 标头很好,以便您可以指定正确的文件名;其他的只是帮助浏览器更好地处理下载,如果您愿意,可以省略。
回答by Frederick
Some edits to this Code to make it work in my case. - For MP3s
对此代码进行一些编辑以使其在我的情况下起作用。- 对于 MP3
You can call this by calling that file filedownload.php- Place it in your server.
您可以通过调用该文件来调用它filedownload.php- 将其放置在您的服务器中。
Call it from a file like - From a WordPress Custom field in this example
从一个文件中调用它 - 从本例中的 WordPress 自定义字段
<a href="<?php bloginfo('url'); ?>/filedownload.php?download=<?php echo get_post_meta($post->ID, 'mymp3_value', true) ?>">MP3</a>
Very simple to do.
做起来很简单。
<?php
$name_of_file = $_GET["download"];
header('Content-Description: File Transfer');
// We'll be outputting a MP3
header('Content-type: application/mp3');
// It will be called file.mp3
header('Content-Disposition: attachment; filename=' .$name_of_file);
header('Content-Length: '.filesize($name_of_file));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
// The MP3 source is in somefile.pdf
//readfile("somefile.mp3");
readfile_chunked($name_of_file);
function readfile_chunked($filename) {
$chunksize = 1*(1024*1024); // how many bytes per chunk
$buffer = '';
$handle = fopen($filename, 'rb');
if ($handle === false) {
return false;
}
while (!feof($handle)) {
$buffer = fread($handle, $chunksize);
print $buffer;
}
return fclose($handle);
}
?>

