Jump to content

Automatic File Deletion


Fmdpa

Recommended Posts

I'm thinking about creating an experimental application. And in it, the user needs to upload an image to test this application. However, I don't want server getting cluttered with hundreds of images, nor do I want to have to manually delete them. How could I either delete them when the user is done, or delete them automatically, say, 4 hours after upload time?

Link to comment
Share on other sites

If you want to delete it when they're "done", then you need to detect that however you do and delete it. You can also use a scheduled task or cron job to clear the directory out periodically. You can also save the files in the temp directory.

Link to comment
Share on other sites

I assume the files will get stored all in the same directory. That'll make life easier no matter what you do. So if this doesn't mess up your plan, you could have your script remove older files before moving the new files into that directory. So User B cleans up after User A and so on.If you're using sessions, you could even eliminate the (possible) problem of User A unlinking his own files if he uploads more than one during a session.

Link to comment
Share on other sites

So if this doesn't mess up your plan, you could have your script remove older files before moving the new files into that directory. So User B cleans up after User A and so on.
Unless, User B comes on and does his thing but User A is not yet done. Then User B will delete files that User A still needs. (And vice versa, if User A uploads after User B has uploaded)
Link to comment
Share on other sites

I was contemplating multiple options. First, I was considering scheduled tasks (on which I am doing some research now). Also, I was considering something involving sessions, although I did not complete a workable plan for that method yet.

I assume the files will get stored all in the same directory. That'll make life easier no matter what you do. So if this doesn't mess up your plan, you could have your script remove older files before moving the new files into that directory. So User B cleans up after User A and so on.
Yes, I was planning to have a directory (or "directories") dedicated to this application. But you have a good idea. I could have a scheduled task that moves files that are older than, say, 12 hours to the "old" directory which would also be cleared at regular intervals.Looking at most hosting cPanels, it looks like cron jobs are allowed, but how can I set one up on localhost?
Link to comment
Share on other sites

On Windows, from "Control Panel > Administrative Tools > Task Scheduler". From then, you create a new task executes PHP from the command line with certain arguments (e.g. the file) on the desired interval.On Linux and MAC... I think the command was "cron", but I have no idea how it's used.

Link to comment
Share on other sites

Would it not be easier to add a couple of lines to your upload script so that it checks for any file in the folder that is older than 4 hours? This way user A can upload several files in a short period of time but none will be deleted. Here's what I use:

// delete any file in this directory that is more than 4 hours old$dir = "./users";if ($d = @opendir($dir)){while (($file = readdir($d)) !== false){$ftime = filemtime($dir.'/'.$file);if (time()-$ftime > 14400) {unlink($dir.'/'.$file);}}closedir($d);}

This will only run after an upload so a file could stay on the server for weeks if no one else uploads something. It also gets round your problem of a user uploading several items in quick sucession.

Link to comment
Share on other sites

I think it would be even easier if you just use the $_SESSION array itself to hold the binary data of the images - as soon as the session is destroyed (i.e. the user is considered "finished" with the task), you destroy the whole session data, including the images themselves. Keep in mind though that you'll load all of the images in PHP's memory on every request for the whole session (regardless of whether you use all of them in every request), so if the user has uploaded 2GB of images where your server has 1GB RAM, you'll likely see a great performance degradation.The only other potential thing to be warned about is the session.save_path ini directive - it should point to a location in which you can save all of the required data, such as the folder you're currently holding the images in. Otherwise, they'll be in the OS' temp folder, which may not be suitable if you have a small system partition and a large data partition (as I keep mine).If the memory sacrifice is too much, you can keep the paths in the session, and set your own save handler that unlinks the images on destroy and/or gc.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...