I am building a web-app that users can upload certain files and work on them through the web-app interface. I need to store these files for the length of a users session. I am creating a folder for each user using the session_id as the folder name and storing the files in there.
The problem: There is nothing to indicate that a user walked away from my site and the session is going out of use. I need a cleanup script that takes the name of each folder and checks if that session_id is still active in order to delete unused and now unreachable folders. How can I do this?
I have had precisely the same issue. My solution was to check for the session file:
Note that this assumes that the
session.save_handlerini setting is set to"files", thesession.save_pathsetting does not have the directory-level prefix (i.e. does not match regex/^\d+;/) and that php’s automatic session garbage collection is enabled. If either of the above assumptions are not true then you should be implementing manual session garbage collection anyway, so can add your clean-up code to that.This also assumes that the only files in
$session_files_dirare your per-session folders and they are all named after their associated session_id.