我将我的应用程序从 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;
}
}