PHP脚本发送zip文件并在不工作后删除
我有一个PHP脚本,它允许用户下载zip中的图像文件夹,这意味着创建zip并将其发送给用户,然后将其从服务器上删除。它工作良好,但有时它不会删除压缩。有什么想法吗PHP脚本发送zip文件并在不工作后删除,php,zip,Php,Zip,我有一个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-
$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);
}
任何帮助都会很好。我相信当连接被主机“强制”中断时会发生这种情况。脚本未完成解析,因此不会出现
unlink()
。我在过去遇到过这个问题,因此您不能依靠脚本来删除该文件
您有几种解决方案。
您可以设置一个数据库并将下载信息保存在那里,然后执行cron任务来删除临时zip
您还可以创建一个cron,检查filemtime()
,如果超过1天,则删除
最后一个选项是我的方法,但我对它做了一些小的调整,并以其他用户无法使用相同下载链接的方式进行了一些下载会话
您可以将
unlink()
与cron任务结合使用。您可能希望使用php zip类,而不是Unix命令zip
,减少跨平台。您的意思是脚本在超过php.ini允许的最大持续时间后被中断?这是一个可能的问题,因为我一直不明白为什么有时候脚本不能100%执行。