View Full Version : How can I delete files at end of $_SESSION?

07-10-2012, 07:05 PM
I'm putting together a site, entirely my own project, where you can process images in all kinds of ways, crop and resize them, add text in any variety of fonts, colors and sizes, add insets of several kinds, make flyers and posters, etc,etc, etc. All of that works and wasn't that hard to write.

But here's my problem, I've set it up so that as soon as someone enters the site they are assigned a name for their working folder and as soon as they upload a picture the folder is made, inside a folder named Pool, then the images are placed inside their folder. But I don't want to store images, I want the folder and its contents to be automatically deleted shortly after their browser is closed and their session ends. How do I do this, I don't know, haven't got a clue, about how to call a function at the end of their session or after it has closed. Because nothing is stored and the site is intended to be free ( except maybe for advanced users ) I see no need for a database.

07-10-2012, 07:13 PM
PHP does not natively support what you want to do.

The only way to do that, is have your users click a link like logout / finish or to run an automated process.

For the latter option you could in your pages use an ajax request to poll a script every 5 seconds. That script then records the time/date access somewhere - either in the session (not a wise idea), file or DB. On the webserver, have a cron job that runs every 5 minutes and wipes out all files that haven't been used for 5 minutes or more.

The reason I say using sessions to record the time is not a good idea is quite a simple one. Your cron script would need to be able to access all the session files for visitors to your site. While thats not particularly difficult on a windows platform, on a shared host under linux it may be very difficult.

07-10-2012, 07:20 PM
maybe use a javascript onclose or beforeclose or something

07-10-2012, 07:40 PM
I know there is an beforeunload (I think thats the right one), but its not a safe thing to use as javascript can be disabled, and you'd just be in a mess.

Javascript should NEVER be used as a safe answer.

07-10-2012, 09:00 PM
you could try sqlite it doesnt use a database but just a file on the server ( its free)


07-10-2012, 09:22 PM
Looking at that it appears to be for c/c++. How does php use that?

07-10-2012, 09:55 PM
Thank you all very much for your replies.

I now know that php doesn't natively have such a function. I plan on having more than one way of doing it, such as a weekly cleanup of all files and folders in Pool. I already have it set so they can delete their own files. People have to use javascript on the site, so I can set it so that if they don't have javascript enabled they can't make a folder or upload. I could maybe put a 'cookie' in their folder with just the unix time when they quit on the cookie, and that can be checked regularly. Anyway, I have some ideas now.

07-11-2012, 12:05 AM
im on my mobile thats the wrong ling lol, i cant remember what its called but the a version of mysql that stores in files instead of a database

07-11-2012, 01:58 AM
im on my mobile thats the wrong ling lol, i cant remember what its called but the a version of mysql that stores in files instead of a database

Erm.. MySQL DOES store in files. Thats how all databases work - they record data into files and then scan through them to find / change data.

What you were thinking of I've no idea, but I've yet to see a version of mysql that writes data on photons..

07-11-2012, 08:22 AM
SQlite was bundled with PHP from 5.0 but they are dropping it back into PECL from 5.4 for reasons I do not know (mad cos with a built in webserver 5.4 would be complete script/server/db all rolled into one... anyways)

I think what Arcticwarrio meant is that with SQLite you could but the database file anywhere you wanted ona per-database/usage basis as opposed to MySQL where you can't change the database path unless you are root.

07-11-2012, 09:54 AM

yeah this one

07-11-2012, 11:48 AM
Have you tried with the javascript codes?