面对PHP脚本下载问题


Facing issue of downloading script in PHP

我正面临下载脚本的问题,每当我的项目网站开始下载时,网站上存在的所有链接都不工作。一旦文件被完全下载,链接开始工作。

我设置header,使用phpreadfile方法下载文件。我希望该文件下载应与用户在网站上的动作同时工作。我使用以下代码

    header("Pragma: public");
    header("Expires: 0");
    header("Content-Type: application/$ctype");
    header("Content-Description: File Transfer");
    header("Content-Disposition: inline; filename=$docName");
    header('Content-Length: '. filesize($filename));
    header("Accept Ranges: bytes"); 
    header("Content-Transfer-Encoding: binary");        
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");            
    readfile("$filename");

这里filename为文件名,ctype为文件类型。

如何解决这个问题?

请让我知道谁有解决这个问题的办法。

我只是猜测你错过了一个适当的标题,改变内容类型和传输编码,因为你需要

$download是实际文件,即/var/www/download_images/1.jpg

$filesize是文件的大小

$filename可以是你想要呈现给用户的任何文件名,例如homer_simpson.jpg

<?php
    if (file_exists($download))
    {
        header("Cache-Control: must-revalidate");
        header("Expires: 0");
        header("Pragma: public");
        header("Content-Description: File Transfer");
        header("Content-Length: ". $filesize .";");
        header("Content-Disposition: attachment; filename=" . $filename);
        header("Content-Type: application/octet-stream; ");
        header("Content-Transfer-Encoding: binary");
        ob_clean();
        flush();
        readfile($download);
    }
?>