在单个脚本中,我需要执行以下操作:
- 从目录中的文件创建一个 zip
- 强制下载新创建的 zip
我的问题是,每当我尝试在单个脚本中执行此操作时,下载的 zip 已损坏(尽管文件大小还可以)。如果我在两个单独的脚本调用中触发进程,下载的 zip 就可以了。
我想问题是在下载开始之前,zip 保存到文件的过程没有完全完成。奇怪的是,在进程之间插入 sleep(3) 并不能解决问题......下面的代码大纲。
如何确保 zip 文件在强制下载开始之前完全完成?
问候/乔纳斯
// 1. create a zip
$createZipFile = new CreateZipFile('temp.zip');
$createZipFile->zipDirectory('temp/', '.');
$createZipFile->saveZipFile();
sleep(3); // <-- Doesn't matter!
// 2. force zip download
$fileServer = new FileServer();
// Line below gives a corrupted zip when run in same script as 1.
$fileServer->forzeDownload('temp.zip');