我有以下代码,它可以在带有 WinRAR 的 Windows 上正常工作,并且在 Mac 上也可以正常工作。但是,由于某种原因,当您使用默认的 Windows 资源管理器打开它时,该 zip 会显示为空,并且当您右键单击并解压时,它会显示该 zip 无效。当用 winrar 或在 Mac 上打开同一个文件时,所有文件都在那里。有任何想法吗?
$passcode = $_GET['passcode'];
$zip = new ZipArchive;
$download = 'download_files_'.$passcode.'.zip';
$zip->open($download, ZipArchive::CREATE);
foreach (glob("../dashboard/uploads/".$passcode."/*.jpg") as $file) { /* Add appropriate path to read content of zip */
$zip->addFile($file);
}
$zip->close();
header('Content-Type: application/zip');
header("Content-Disposition: attachment; filename = $download");
header('Content-Length: ' . filesize($download));
header("Location: $download");
Run Code Online (Sandbox Code Playgroud)
使用以下方法可以轻松修复此问题:我必须将生成的 zip 文件移至上传文件夹内并删除,../dashboard/uploads/因为正是此路径导致 Windows 将其视为损坏的文件