I am allowing users to upload a profile picture of themselves. It involves these steps:
- User chooses an image file to upload
- Server receives the file through AJAX and stores with a name of
temp_userid.jpg
- Server returns the image location to the page so the user can see it and make adjustments
- The user clicks Save Changes which posts back to the server as final confirmation of the image update
- Server loads the
temp_userid.jpg
, makes the adjustments e.g scale and crop, overwrites any existing profile image, and then finally deletes thetemp_userid.jpg
image
This works fine as long as the user saves his changes. If the user decides to leave the page, then the temp_userid.jpg
file is still on the server and is never deleted. Some of the images can be over 5MB in size which I don't want on the server.
How could I go about cleansing the folders of temp files in a safe manner? I was thinking I could:
- Attempt to delete the temp image file whenever the user logs in to his account. The only slight issue here is that the user may never log-in again. But that could be dealt with using some kind of account expiration check.
- Attempt to delete all images with a prefix of 'temp_' from the server periodically. The risk here is that it deletes it while a user is still making adjustments to his image which would be terrible!
Are there any better ways of doing this? I'm tagging this as ColdFusion because thats the application server I'm using.