我有一个 PHP 脚本,用于允许用户下载一个包含多张图片的文件夹,以 zip 格式打包后发送给用户,并从服务器上删除该文件。现在它已经可以正常工作,但有时候无法成功删除该 zip 文件。你有什么想法吗?
$filename_no_ext=$_GET['zip'];
// we deliver a zip file
header("Content-Type: archive/zip");
// filename for the browser to save the zip file
header("Content-Disposition: attachment; filename=$filename_no_ext".".zip");
// get a tmp name for the .zip
$tmp_zip = tempnam ("tmp", "tempname") . ".zip";
// zip the stuff (dir and all in there) into the tmp_zip file
`zip -r $tmp_zip $filename_no_ext`;
// calc the length of the zip. it is needed for the progress bar of the browser
$filesize = filesize($tmp_zip);
header("Content-Length: $filesize");
// deliver the zip file
$fp = fopen("$tmp_zip","r");
echo fpassthru($fp);
fclose($fp);
// clean up the tmp zip file
//`rm $tmp_zip `;
unlink($tmp_zip);
//If user aborts check they have and then clean up the zip
ignore_user_abort(true);
echo "\n";
if (connection_status()!=0){
unlink($tmp_zip);
}
if (connection_aborted()) {
unlink($tmp_zip);
}
任何帮助都将是极好的。
zip
,因为它更少跨平台。http://www.php.net/manual/en/book.zip.php - Clement Herreman