PHP - 简单的下载脚本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12295604/
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
PHP - simple download script
提问by skolind
I'm making a simple download script, where my users can download their own images etc.
我正在制作一个简单的下载脚本,我的用户可以在其中下载他们自己的图像等。
But I'm having some weird problem.
但我遇到了一些奇怪的问题。
When I've downloaded the file, it's having the contents from my index.php file no matter what filetype I've downloaded.. My code is like so:
当我下载文件时,无论我下载什么文件类型,它都会包含我的 index.php 文件中的内容。我的代码是这样的:
$fullPath = $r['snptFilepath'] . $r['snptFilename'];
if (file_exists($fullPath)) {
#echo $fullPath;
// setting headers
header('Content-Description: File Transfer');
header('Cache-Control: public'); # needed for IE
header('Content-Type: '.$r['snptFiletype'].'');
header('Content-Disposition: attachment; filename='. $filename . '.' . $r['snptExtension']);
header('Content-Length: '.$r['snptSize'].'');
readfile($fullPath)or die('error!');
} else {
die('File does not exist');
}
$r is the result from my database, where I've stored size, type, path etc. when the file is uploaded.
$r 是我的数据库的结果,当文件上传时,我在其中存储了大小、类型、路径等。
UPDATE
更新
When I'm uploading and downloading *.pdf files it's working with success. But when I'm trying to download *.zip and text/rtf, text/plain it's acting weird.
当我上传和下载 *.pdf 文件时,它工作成功。但是当我尝试下载 *.zip 和 text/rtf, text/plain 时,它表现得很奇怪。
By weird I mean: It downloads the full index.php file, with the downloaded file contents inside of it.
奇怪的是我的意思是:它下载完整的 index.php 文件,其中包含下载的文件内容。
ANSWER
回答
I copied this from http://php.net/manual/en/function.readfile.phpand it's working now. It seems that : ob_clean(); did the trick! Thanks for the help everyone.
我从http://php.net/manual/en/function.readfile.php复制了它,现在它正在工作。似乎: ob_clean(); 成功了!谢谢大家的帮助。
#setting headers
header('Content-Description: File Transfer');
header('Content-Type: '.$type);
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;
回答by Erdin? ?orbac?
Try this function , or implement these headers to your code
试试这个函数,或者在你的代码中实现这些头文件
function force_download($filename) {
$filedata = @file_get_contents($filename);
// SUCCESS
if ($filedata)
{
// GET A NAME FOR THE FILE
$basename = basename($filename);
// THESE HEADERS ARE USED ON ALL BROWSERS
header("Content-Type: application-x/force-download");
header("Content-Disposition: attachment; filename=$basename");
header("Content-length: " . (string)(strlen($filedata)));
header("Expires: ".gmdate("D, d M Y H:i:s", mktime(date("H")+2, date("i"), date("s"), date("m"), date("d"), date("Y")))." GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
// THIS HEADER MUST BE OMITTED FOR IE 6+
if (FALSE === strpos($_SERVER["HTTP_USER_AGENT"], 'MSIE '))
{
header("Cache-Control: no-cache, must-revalidate");
}
// THIS IS THE LAST HEADER
header("Pragma: no-cache");
// FLUSH THE HEADERS TO THE BROWSER
flush();
// CAPTURE THE FILE IN THE OUTPUT BUFFERS - WILL BE FLUSHED AT SCRIPT END
ob_start();
echo $filedata;
}
// FAILURE
else
{
die("ERROR: UNABLE TO OPEN $filename");
}
}
回答by skolind
I copied this from http://php.net/manual/en/function.readfile.phpand it works now. ob_clean(); did the trick..
我从http://php.net/manual/en/function.readfile.php复制了它,现在它可以工作了。ob_clean(); 成功了。。
#setting headers
header('Content-Description: File Transfer');
header('Cache-Control: public');
header('Content-Type: '.$type);
header("Content-Transfer-Encoding: binary");
header('Content-Disposition: attachment; filename='. basename($file));
header('Content-Length: '.filesize($file));
ob_clean(); #THIS!
flush();
readfile($file);

