Destroy files when session will destroy in php
Asked Answered
T

5

1

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.

Tour answered 21/7, 2011 at 6:19 Comment(0)
S
2

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.

Sevier answered 21/7, 2011 at 6:41 Comment(0)
I
0

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.

Impugn answered 21/7, 2011 at 6:27 Comment(10)
ya i know that its simple. but the client requirement to do like this. there will no possible to predict when the session going to destroy.Tour
exactly, you cannot tell if the customer is gone or the browser crashed or power goes out. Whatever happens, you do not save the file on the project directory. But as soon as the customer logs in, get the file from the saved file path and get the file to the correct position.Impugn
ya you're correct. but the problem is i need to save all the paths into db, what user uploading too. i need to show the saved records from db. not only the session.Tour
Told you so, Its very dependent on the BL you are working on. Why are you saving the filepath in DB before the user is logged in. You might require the USER_ID before saving that file. So What i recommend that save the file on the Log in action instead. And before that use a temporary folder for such files. And as soon as the user logs in, get the file from temporary folder and save it to the DB or project folder.Impugn
good here we used to set the user id is 0 when he is not logged in. still i cant understand what is the diff to store in temp folder or inside of project folder. we can delete the files from any where na.? isn't it? are you saying to clear or delete the temp folder when user close ? if we need to create every time temp folder means it'll too critical because of we cant say how many user will enter in to same time, at the same time we cant unlink the file for every user. it can done by only stored values in db??Tour
Why we cant delete from project folder? If its possible to delete from temp means why we cant from project folder? Please explain ?Tour
No, Keep the common temp folder for everyone. The idea here to separate the folder is not to corrupt the project directory with unknown file entry. If the file is in temp folder you dont need to check if the user browser is closed or what. And you get the file in a required place after the user logs in. On the other hand, If you put the file in the project file what would you do if the user closes the browser. You would preferebly run some sort of ChronJobs to delete these ambiguous files.Impugn
@Why we cant delete from project folder? What will you delete from the project folder and on what basis is the big question here. The main problem here is that how we know that the user closed his browser. Well again its up to your implementation and Business Requirement that how you want to delete the file.Impugn
ok thank u for spend the time to my question.thank u once againTour
here we just store the file name and paths in to db.. we have already done it by manually that if we click remove button it'll remove from db as well as path unlink. so we can remove it easily. but the problem is how we can find the browser going to close and how to deleting files .....Tour
L
0

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

Led answered 21/7, 2011 at 6:37 Comment(0)
E
0

use session set save handler to create an event handler for session events.

Eiffel answered 26/11, 2011 at 9:3 Comment(0)
H
-2

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..

Hindquarter answered 21/7, 2011 at 6:28 Comment(6)
just found other similar posts. Check this out. https://mcmap.net/q/1175290/-php-session-expire-eventHindquarter
ya fine santhose. here is the problem we need to delete files before session gets destroy. then how we can predict about the session destroy?Tour
@Sriram: I just did a bit of search on this and seems its not as straightforward in php as it is in java. The server does not keep track of session on it 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.Hindquarter
There are different solutions people use for this. Few of them are listed in this post. Please check. #1287564.Hindquarter
ya trying santhose and let you know thank u for this effort .Tour
"The server does not keep track of session on it side" - that's not entirely true (though it still may be a show stopper in this case). There is a session garbage collector that is invoked once in a while to collect old sessions that haven't been "actively" destroyed. see docs.php.net/manual/en/…Ellisellison

© 2022 - 2024 — McMap. All rights reserved.