0

Hello I use the following script for downloading files with php:

$archive = tempnam("//somepath/mms_1/MMS_SERVICE/","asset");
$zipobject = new ZipArchive();
$zipobject->open($archive, ZIPARCHIVE::CREATE);
$zipobject->addFile('somefile', '');
......
$zipobject->close();

$filesize = filesize($archive);

$timestamp=date('d-m-y_H-i');
header("Content-type: application/x-zip-compressed");
header("Content-Disposition: attachment; filename=Assets_$timestamp.zip  ");
header("Content-Description: Download PHP");
header("Content-Length: filesize ");
header("Content-Transfer-Encoding: binary");
readfile_chunked($archive);
unlink($archive);

The problem is that php do not delete the temp zip file when finish downloading, or when user aborts downloading, or when leave page. It seems that unlink never executes in this cases ignore_user_abort(true); function but this doesnt resolve the problem. Any one can suggest a solution for this?

albanx
  • 6,193
  • 9
  • 67
  • 97
  • What's the code for `readfile_chunked();`? If it opens a file pointer are you sure it's being closed again? – Leigh Feb 22 '11 at 11:58
  • It's similiar to readfile(), but even using readfile gives the same problems. – albanx Feb 22 '11 at 12:06
  • 1
    You can try with the solution described here [1]: http://stackoverflow.com/questions/2641667/deleting-a-file-after-user-download-it – fath Jul 03 '12 at 09:02

3 Answers3

0

Where did you specified ignore_user_abort(true); ? It need to be before readfile

0

Try to use the register-shutdown call in php to have your cleanup function always run.

regilero
  • 29,806
  • 6
  • 60
  • 99
0
$filesize = filesize($archive);  
$timestamp=date('d-m-y_H-i'); 
header("Content-type: application/x-zip-compressed"); 
header("Content-Disposition: attachment; 
filename=Assets_$timestamp.zip  "); 
header("Content-Description: Download PHP"); 
header("Content-Length:" **.$filesize** ); 
header("Content-Transfer-Encoding: binary"); 
if ($archive != ''){ 
 $fileData = @file_get_contents($archive);
 echo $fileData;
 unlink($archive);
}

try the suggestion above. I have not attempted your version, but this works for me, so barring any access issues, it should work for you too. (I have marked in bold (double stars)the code I have changed from your version)

Nepa
  • 1