doushouhe7072 2019-07-04 13:17
浏览 181

使用ZipArchive下载空/损坏的zip文件

I'm using ZipArchive to zip and download the dynamically generated pdf files. The files are getting generated in the designated folder but when I'm trying to download the zip file, the 1KB broken file is getting download.

I've tried this code at my local wampp and here it works perfectly and the proper zip file is getting generated but in production the broken zip is getting downloaded. I believe there's some server variable which might be resulting this?

// creating directory in the server with timestamp
$dirName = strtotime(date('Y-m-d H:i:s'));
$dirPath = "/var/www/...../foldertobezipped/" . $dirName;
mkdir($dirPath);

// generating and storing the generated pdf files in the $dirPath folder
foreach ($resultArray as $filelist) {
    $this->invoicepdfview($orderid);
}

// zip file name
$archive = $dirName . '.zip';

// invoking ZipArchive
$zip = new ZipArchive;
$zipPath = $dirPath . "/" . $archive;
$zip->open($zipPath, ZipArchive::CREATE);

// scanning all the files to be zipped
$files = scandir($dirPath);

// removing the unwanted key-value from the array
unset($files[0], $files[1]);

// adding files to the zip file
foreach ($files as $file) {
    $new_filename = substr($dirPath . '/' . $file, strrpos($dirPath . '/' . $file, '/') + 1);
    $zip->addFile($dirPath . '/' . $file, $new_filename);
}
$zip->close();

// downloading the zipped file
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename=' . $zipPath);
header('Content-Length: ' . filesize($zipPath));
readfile($zipPath);
unlink($zipPath);

I've checked and compared most of the php.ini variables which could affect this between local WAMPP and production server. Could someone help me out of this? Where am I missing something?

P.S. - I've provided 777 permission to the directory where the files are being created.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 对于相关问题的求解与代码
    • ¥15 ubuntu子系统密码忘记
    • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
    • ¥15 保护模式-系统加载-段寄存器
    • ¥15 电脑桌面设定一个区域禁止鼠标操作
    • ¥15 求NPF226060磁芯的详细资料