开发者

File Download Not Working

When I download the original zip it works fine, but when I download it using the below headers and stuff it doesn't work. I know it's better to take this route and tell the browser how to handle the file rather than leave it up to the browser, but I can't get this to work, so I'm tempted to use a header() forward.

            $path = $this->tru->config->get('root.path').'/Digital Version of Book for Web.zip';

            set_time_limit(0);
            header("Cache-Control: public");
            header("Content-Description: File Transfer");
            header('Content-Type: application/zip');
            header("Content-Transfer-Encoding开发者_Python百科: binary");
            header('Content-Disposition: attachment; filename="NewFileName.zip"');
            header('Content-Length: ' . filesize($path));

            $f = fopen($path, 'rb');
            fpassthru($f);
            fclose($f);

Edit:

Sorry, what I mean by it doesn't work is that the file downloads in a zip format (all 9.3 MB) but I'm unable to unpackage the zip because it's invalid.


Take a look into the ZIP file using Notepad or another text editor. Check whether there is a PHP error message screwing up the file on the first few lines. It could be a "headers already sent" message or the set_time_limit() call throwing an error due to the script being in safe mode.


Try using readfile(). An example is provided in the PHP Manual.

$file = 'monkey.gif';

if (file_exists($file)) {
    header('Content-Description: File Transfer');
    header('Content-Type: application/octet-stream');
    header('Content-Disposition: attachment; filename='.basename($file));
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Pragma: public');
    header('Content-Length: ' . filesize($file));
    ob_clean();
    flush();
    readfile($file);
    exit;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜