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 win11修改中文用户名路径
    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
    • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
    • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
    • ¥15 帮我写一个c++工程
    • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
    • ¥15 关于smbclient 库的使用
    • ¥15 微信小程序协议怎么写
    • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
    • ¥20 怎么用dlib库的算法识别小麦病虫害