Delete a file at shutdown
Posted: Mon Feb 03, 2003 1:45 am
Hello all
I am creating a ZIP file on the fly, storing it in a temporary directory, delivering this file to the user (with readfile) and then finally deleting the temporary ZIP file:
This works perfectly as long as the file is downloaded successfully. Should the user click on cancel in the browsers “save as” dialog box, the temporary file is not deleted.
I understand that I could implemented some garbage collection, but I was wondering whether it is possible to use PHP’s register_shutdown_function() function to delete the file when the execution of the download script is interrupted.
Or is there some other way to delete a file when the execution of a script is unduely terminated?
Thanks in advance
Sam
I am creating a ZIP file on the fly, storing it in a temporary directory, delivering this file to the user (with readfile) and then finally deleting the temporary ZIP file:
Code: Select all
// pop up 'save as' dialog
if (preg_match("/MSIE/",$_SERVER['HTTP_USER_AGENT'])
AND !preg_match("/Opera/",$_SERVER['HTTP_USER_AGENT']))
header("Content-Disposition: filename=".$file_zip);
else
header("Content-Disposition: attachment;filename=".$file_zip);
header("Content-Type: application/octetstream");
header("Content-Transfer-Encoding: binary");
header("Pragma: no-cache");
header("Expires: 0");
header("Content-Length: ".filesize($file_temp)."");
readfile($file_temp);
// delete temp file
unlink($file_temp);I understand that I could implemented some garbage collection, but I was wondering whether it is possible to use PHP’s register_shutdown_function() function to delete the file when the execution of the download script is interrupted.
Or is there some other way to delete a file when the execution of a script is unduely terminated?
Thanks in advance
Sam