繁体   English   中英

PHP下载.zip文件但错误

[英]PHP downloading .zip file but incorrectly

我正在尝试从本地服务器将zip文件下载到HD。

$zip = new ZipArchive;
$zip->open($archive, ZipArchive::CREATE);

$files = scandir($dir);
unset($files[0], $files[1]);
foreach ($files as $file) {
$zip->addFile($dir.'/'.$file);
}
$zip->close();
$download=$archive;
header("Content-Disposition: attachment; filename=\"Reports". $stocktake_id. ".zip");
header("Pragma: public");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

已下载文件,但文件大小不正确且无法正确打开,表示格式无效或文件已损坏。

我认为您缺少内容类型标头。 另外,我看不到您将原始数据回显到客户端吗? 并且,请确保没有调试输出或文件本身中没有的任何内容。

//After you saved your file:

download_file_to_client($saved_file);
exit;

function download_file_to_client($file, $mime = 'application/zip')
{
    $filesize = filesize($file);
    header('Content-Type: '.$mime.';charset=utf-8');
    header('Content-Disposition: attachment;filename="some-filename.zip"');
    header('Pragma: no-cache');
    header('Expires: 0');
    header('Content-Length: ' . $filesize);
    flush(); // Get the headers out immediately to prevent any delay in some browsers.

    $fh = fopen($file, "r");
    while ($fh && !feof($fh))
    {
        echo fread($fh, 8192);
    }
    fclose($fh);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM