我将我的应用程序从 WAMP 服务器(PHP 版本 <= 5.2,Windows XP)移动到 LAMP 服务器(PHP > 5.3,Ubuntu 12.04 LTS),我现在遇到的问题是我的下载功能输出文件在屏幕上并且它不会强制浏览器执行下载。
谁能给我指出正确的方向?
代码如下:
function download()
{
$fullPath = $_POST['fisier'];
if($fullPath=="NULL")
{
echo "Wrong path!";
}
else
{
$fd = fopen ($fullPath, "r");
if ($fd) {
$fsize = filesize($fullPath);
$path_parts = pathinfo($fullPath);
$ext = strtolower($path_parts["extension"]);
switch ($ext) {
case "pdf":
header("Content-type: application/pdf");
break;
case "tiff":
header("Content-type: image/tiff");
break;
case "tif":
header("Content-type: image/tiff" );
break;
default:
header("Content-type: application/force-download");
break;
}
header("Content-Description: File Transfer");
header('Content-Disposition: attachment; filename="'.$path_parts["basename"].'"');
header("Content-length: ".$fsize);
header("Cache-control: private"); //use this to open files directly
ob_clean();
flush();
while(!feof($fd)) {
$buffer = fread($fd, 2048);
echo $buffer;
}
}
fclose ($fd);
exit;
}
}
最佳答案
您可以根据 PHP docs 使用 Content-Disposition
header :
If you want the user to be prompted to save the data you are sending, such as a generated PDF file, you can use the » Content-Disposition header to supply a recommended filename and force the browser to display the save dialog.
// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');
关于php - 从 WAMP 服务器更改为 LAMP 服务器后,文件下载脚本不起作用。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15511896/