Jump to content

Recommended Posts

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?  ???

 

Link to comment
https://forums.phpfreaks.com/topic/50672-delete-uploaded-file-when-user-exits/
Share on other sites

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...

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.