问题
is it possible to remove files from the folder when session will destroy. am doing that when a user come into the site and he can upload files(images, or textfiles), etc.. with out login into the site. and the files 'll store into my project's folder. now i need to do if the user quits from browser with out login i need to delete all the files what he upload in to project folder. how to do this ?
Thanks in advance.
回答1:
You can do this by implementing your own session handler. This way, you can define a callback for various events, including the destruction of a session. See this link for more information:
http://www.php.net/manual/en/function.session-set-save-handler.php
Update: The problem with that solution is that you need to implement the rest of the session handling code as well (initialize the session, close the session, read from storage, write, garbage collect). However, the linked page above gives a full example that you can add your functionality to.
回答2:
It all depends on the business decision.
You can do, Do not save the file in projects folder instead save it somewhere else like 'tmp' folder and save the reference of that file (file path) in session.
回答3:
by unlink("path") function you can do it in PHP .. but it also depends on the logic that you have used.
if you just want to delete uploaded file then unlink is your solution
回答4:
use session set save handler to create an event handler for session events.
回答5:
Sriram, In PHP, seems the server does not keep track of session on its side. Only time the server knows of a session is expired is when it receives the info about the session cookie in the request. If the cookie is there session exist or else, its expired..
来源:https://stackoverflow.com/questions/6772043/destroy-files-when-session-will-destroy-in-php