nzlyoung Posted May 9, 2007 Share Posted May 9, 2007 Hi, I've been working for this simple project. User fills out the application form (uploads a file), redirected to the confirmation page, make a payment, redirected to the payment confirmation page and then it sends an email with this file to the customer or admin. There is no user authentication so it could be anybody uploading files. The problems is that I want to delete the uploaded file when the user exits the browser or click onto some other page when the user decides to withdraw from making a payment. Because this temporary file becomes permenant. Of course you can delete the files manually using ftp but it could be a hassle for the end user. I've been thinking about session_set_save_handler, register_shutdown_function or connection aborted functions but none seems to work in this situation. Any suggestion? ??? Quote Link to comment https://forums.phpfreaks.com/topic/50672-delete-uploaded-file-when-user-exits/ Share on other sites More sharing options...
trq Posted May 9, 2007 Share Posted May 9, 2007 Why not just delete the file after the email is sent? Otherwsie, yeah, Id'e be looking at a custom session handler. Quote Link to comment https://forums.phpfreaks.com/topic/50672-delete-uploaded-file-when-user-exits/#findComment-249074 Share on other sites More sharing options...
nzlyoung Posted May 9, 2007 Author Share Posted May 9, 2007 Yeah, the file is going to be deleted after the email is sent. the thing is that after uploading file it will be redirected to the another page (to make a payment or to show the confirmation page). so if the user exits the browser or decides not to proceed with the payment after uploading file, the file will just stay there... :-\ and I want to clear these temporary files... Quote Link to comment https://forums.phpfreaks.com/topic/50672-delete-uploaded-file-when-user-exits/#findComment-249087 Share on other sites More sharing options...
witakr Posted May 9, 2007 Share Posted May 9, 2007 I would think that a simple session handler and unlink() would work, right? Quote Link to comment https://forums.phpfreaks.com/topic/50672-delete-uploaded-file-when-user-exits/#findComment-249094 Share on other sites More sharing options...
Wildbug Posted May 9, 2007 Share Posted May 9, 2007 What about a crontab entry on your system that runs once a day, deleting tmp files that are older than, say, 24 hours? In crontab: find /your/dir/tmp -mtime +1 --exec rm {} \; Quote Link to comment https://forums.phpfreaks.com/topic/50672-delete-uploaded-file-when-user-exits/#findComment-249107 Share on other sites More sharing options...
nzlyoung Posted May 10, 2007 Author Share Posted May 10, 2007 Hi Wildbug, Thanks for the info. The crontab is one of the solutions that will work for this situation although I don't have access to Linux. kk. Quote Link to comment https://forums.phpfreaks.com/topic/50672-delete-uploaded-file-when-user-exits/#findComment-249488 Share on other sites More sharing options...
Wildbug Posted May 10, 2007 Share Posted May 10, 2007 Well, along those same lines, you could write a PHP function that finds files older than a certain time and deletes them. function delete_old_files($max_age_in_hours) { // (Untested) $files = glob("/your/dir/tmp"); foreach ($files as $file) if (3600*(time()-filemtime($file)) > $max_age_in_hours) unlink ($file); } // ..... delete_old_files(24); // run every time // ...or... if (date('G') == '16') delete_old_files(24); // run during the 4pm hour. The thing is, this function doesn't need to be run when a particular user exits; it can be run for any user, and older files will be deleted. How you might choose to decide when to run the function will vary on how busy your site is. You could put it in the login script for a slower site so it gets run on each log in, or only run it during the 4pm hour on a busier site. It's not a perfectly efficient solution, but if your options are limited, it is effective. Quote Link to comment https://forums.phpfreaks.com/topic/50672-delete-uploaded-file-when-user-exits/#findComment-249772 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.