In my website, the user can upload a profile image. The website structure is as follows:
C:\xampp\htdocs
C:\xampp\upload\user1
For security reason, the upload file has been moved into folder C:\xampp\upload\user1\.
The question is how I can serve this image later to the user. In other words, after uploading the image, the user should be able to see the upload image on the web page. What should I do?
Here is what I can figure out
- Introduce a new folder
C:\xampp\htdocs\picture\randomimagename
which is used to temporarily save the user’s picture. - Use the link to
C:\xampp\htdocs\picture\randomimagenameand serve it to the user.
Now the problem is when should I delete this image?
- when the user logouts out. yes, I know how to do this.
- when the user closes the browser. how do I know this?
- any other cases.
Save the image to
C:\xampp\htdocs\picture\randomimagenameand then every once in a while, have a Windows Scheduled Task clean it out (e.g. when it’s been more than 15 minutes since it was created).That way you don’t have to worry about doing it when the user logs out, or when the user closes the browser, or anything else. Tweak how frequently the job runs depending on how much you care about disk space vs how much you care about CPU usage/IO.
By the way, I hope you’re not doing things like:
that will only work when run on the server which Apache is running on. You want to use a proper path, relative to your site’s root.