php - 从 WAMP 服务器更改为 LAMP 服务器后,文件下载脚本不起作用。

标签 php download lamp

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

相关文章:

php - 使用 wkhtmltopdf 静态二进制文件时找不到 libjpeg.so.8

php - HMVC 模块阵列 Codeigniter

python - 使用 Python 下载共享的 Google Drive 文件夹

java - Windows 服务器人员想要一种简单的方法迁移到 Linux/Java(我猜是 LAMP)

php - 如果我要在数据库中存储两条记录,则需要计量单位表

php - 将数组插入mysql数据库

php - 无法通过 PHP 连接到 docker mysql

html - 如何使用 HTML/JavaScript 强制下载?

delphi - 使用 Delphi XE 将通过 WinHTTP 下载的文件保存到磁盘

在 Ubuntu 18.04 LTS 上全新安装 VirtualMin 后,Wordpress 网站显示 php 代码