从WAMP服务器更改为LAMP服务器后,文件下载脚本无法正常工作。

I moved my application from a WAMP server (PHP version <= 5.2, Windows XP) to a LAMP server (PHP > 5.3, Ubuntu 12.04 LTS), and the problem that I have now is that my download function outputs the file on the screen and it's not forcing the browser to perform a download.

Anyone can point me in the right direction?

Here's the code:

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;
}
}

try using header("Content-type: application/octet-stream"); instead of header("Content-type: application/force-download");

The recommended action for an implementation that receives an "application/octet-stream" entity is to simply offer to put the data in a file

From here. Looks like your application/force-download is non standard.

You can use the Content-Disposition header as per the PHP docs:

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"');