Reputation: 818
Yes, I have an issue.
I have my upload profile picture.
1) A popup comes up
2) you upload a image
3) you crop it
4) you press finish, and you get it as your new profile photo (the original uploaded image will be replaced with the cropped one, so the original image will be removed, and will not take any space)
Now if the users cancel or leave this procedure after 2), where the image is uploaded, then the original fullsize image will remain on the server. I thought OK, what about if he closes the popup, warn him, and then remove the image. Then what if the user crashes his browser, leaves the site, or other weird cases. That will still make it remain on the server.
So what can I do about this? I thought about a temp dir, deleting everything in it after xx time, but is this even possible to make without a cronjob? And does there exist an better solution than this, for my issue?
Upvotes: 2
Views: 78
Reputation:
This shouldn't be terribly difficult. PHP will upload to a temporary directory and you must move the file into your desired directory from the temporary location. So, to avoid having images go over before the process is complete, pass the variable and don't do the final move of the file from the temp directory until you're at the end of the process. I personally like to do all the DB Updates, etc upon completion of a multi-step process, just in case they should decide to back out.
Cleanup --could-- be a cron job. But honestly, even on large sites I've not encountered a situation where you couldn't just occasionally do it by hand if required. With more than 1000 users per week on one site, we NEVER had any space issues with profile pictures.
Upvotes: 0
Reputation: 72510
You can't rely on anything like running scripts when the window closes because although it may work for most users, there are many scenarios where that would end up ignored or whatever.
The best solution is, as you say a cron job. If you are on shared hosting and cannot set one up on the server, write a PHP script that will check images and delete any older than a few hours.
Then set up a scheduled task on your own computer or somewhere else to periodically open that web page. (On Linux you can set up a cron job that uses wget
.)
Upvotes: 1
Reputation: 25053
Having a directory where items age out is normal and reasonable, for exactly the situation you describe. Users disappear unexpectedly, and you have to throw out the trash they left behind.
Do you have a session key of some kind? You could tie garbage collection to when the session key expires.
Upvotes: 1