Delete file after user upload

I use this to send the file to the user

header('Content-type: application/zip'); header('Content-Length: ' . filesize($file)); header('Content-Disposition: attachment; filename="file.zip"'); readfile($file); 

I want to delete this file after user downloads, how can I do this?

EDIT: my script is like this, when the user presses the download button, my script will create a temporary zip file and the user will upload it, after which the temporary zip file will be deleted.

EDIT2: OK. The best way seems to be running a cron job that will clean up temporary files once per hour.

EDIT3: I tested my script using unlink , it works if the user does not cancel the download. If the user cancels the download, the zip file will remain on the server. So that’s enough. :)

EDIT4: WOW! connection_aborted() did the trick!

 ignore_user_abort(true); if (connection_aborted()) { unlink($f); } 

This file will be deleted even if the user cancels the download.

+47
php
Apr 14 '10 at 23:08
source share
7 answers
 unlink($filename); 

This will delete the file.

It should be combined with ignore_user_abort() Docs so that unlink is still running even the user canceled the download.

 ignore_user_abort(true); ... unlink($f); 
+27
Apr 14 '10 at 23:10
source share

I always use the following solution:

 register_shutdown_function('unlink', $file); 
+12
Oct. 15 '15 at 14:26
source share

There is no correct way to determine if a file has been fully downloaded by the user or not.
Thus, the best way is to delete the file after a period of inactivity.

+8
Apr 14 '10 at 23:17
source share

I also have very similar functionality on one of my sites. It will be like deleting a randomly created folder and zip file after / canceling the download. I'm trying to explain it here, maybe someone finds it useful.

skin.php:
This page contains a download link, such as "http://mysite.com/downoload/bluetheme"

.htaccess:
I have a rule in a htaccess file to redirect a download request to a php file. [Download.php].

 RewriteRule ^download/([A-Za-z0-9]+)$ download.php?file=$1 [L] 

download.php:

 include "class.snippets.php"; $sn=new snippets(); $theme=$_GET['file']; $file=$sn->create_zip($theme); $path="skins/tmp/$file/$file.zip"; $config_file="skins/tmp/$file/xconfig.php"; $dir="skins/tmp/$file"; $file.=".zip"; header("Content-type: application/zip"); header("Content-Disposition: attachment; filename=$file"); header("Pragma: no-cache"); header("Expires: 0"); readfile($path); //remove file after download unlink($path); unlink($config_file); rmdir($dir); 

therefore, upon request, download.php will create a directory with a random name using the snippets class. Inside the directory, a zip file will be created. after downloading / canceling the request, all files and the directory will be deleted.

+3
01 Oct '12 at 6:20
source share

I could not find something that worked for me, so I came up with this that seems to work well for me:

 header('Content-type: application/zip'); //this could be a different header header('Content-Disposition: attachment; filename="'.$zipName.'"'); ignore_user_abort(true); $context = stream_context_create(); $file = fopen($zipName, 'rb', FALSE, $context); while(!feof($file)) { echo stream_get_contents($file, 2014); } fclose($file); flush(); if (file_exists($zipName)) { unlink( $zipName ); } 

I hope this helps someone

+3
Apr 24 '14 at 20:01
source share

connection_aborted() never worked for me. Although ob_clean() works exactly as it should. Hope this helps others

 header('Content-type: application/pdf'); header('Content-Disposition: inline; filename="' . $file . '"'); header('Content-Transfer-Encoding: binary'); header('Accept-Ranges: bytes'); ob_clean(); flush(); if (readfile($file)) { unlink($file); } 
+1
May 21 '15 at 5:54
source share

The safest way I can think of is to use some kind of flash movie on the client side that can watch the transfer from the client side and then make an XmlHttpRequest call to the server after the download is complete.

AFAIK, there is no way to do this reliably without using Flash (or a similar browser plugin)

0
Apr 14 '10 at 23:25
source share



All Articles