?
Solved

PHP - Unlink Timer - Delete files after * mins

Posted on 2007-08-01
7
Medium Priority
?
2,305 Views
Last Modified: 2013-12-13
Hi!
I have a php page that allows a user to download a unique file. ( based on random number )

The user can download. But i wanted this file to be deleted like 30 mins.
Something like a timer for such files...
EG: User 1 download file qwert.zip at 1200hrs. File to be deleted @ 1230hrs.
User 2 download file zxcvb.zip at 1201hrs. File to be deleted @ 1231 hrs.

Is this workable in php?
I know of the command to delete = unlink()
but don know about the timer...

Please advice.
Cheers!
Darence
0
Comment
Question by:darenceang
  • 4
  • 2
7 Comments
 
LVL 4

Expert Comment

by:DigitalTyrant
ID: 19611130
I have written a similar solution.  I have set the scheduler on the server to run a script, my maintenance script, every 30 mins.  This maintenance script monitors a specific folder, deleting all files that were created more than 24 hours prior.  It's not deadly accurate on the 24 hours, leaving some files almost 24 hours and 30 mins, but it works well to keep my cache clean.
0
 
LVL 9

Expert Comment

by:tolgaong
ID: 19611136
if (filectime($file)+(30*60)<time())  unlink($file);
file change time or you can use filemtime for modify time
0
 
LVL 4

Expert Comment

by:DigitalTyrant
ID: 19611149
In your example, you could setup your script to run more often and be more accurate.  Depending on the size of the zips in question.  Just make sure that the script has enough time to delete all the pending files before it gets called again.  
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 4

Author Comment

by:darenceang
ID: 19617907
Hi DigitalTyrant..
May i have a look at the script?

Please advice.
Cheers!
Darence
0
 
LVL 4

Accepted Solution

by:
DigitalTyrant earned 200 total points
ID: 19618615
here's a quick sample...
<?
$dir = "/path/to/website/downloads/";
if (is_dir($dir)) {
  if ($dh = opendir($dir))
  {
    while (($file = readdir($dh)) !== false)
    {
      $mtime = filemtime($file);
      if(($mtime+1800)<time())
      {
        delete($file);
      }
    }
    closedir($dh);
  }
}
?>

Hope this helps.
0
 
LVL 4

Author Comment

by:darenceang
ID: 19636586
Hi Digital Tyrant!
Thanks for the advice.
Actually i have a "download" php form that submits the user name
Upon clicking, the form action will go to ./download.php?user=darenceang
It will process and prompt user to download the file.

Am i right to say that your codes go into the ./download.php ? and not another php script?
If i put ur codes in the ./download.php, will it sort of "hang there" while waiting for the time to run out?

Pleae advice.
0
 
LVL 4

Expert Comment

by:DigitalTyrant
ID: 19639933
I wouldn't recommend putting the code in your download.php file.  There are a couple reasons: first, if the script has to run through thousands of files, it would hang on the clients side until it completed, and second, if you only have one visitor an hour, the files would reside on the server longer than the half hour limit it is designed for.

I suggest creating a "purge.php" file that contains the script and then add to the scheduler to run every 5-10 minutes.  
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses four methods for overlaying images in a container on a web page
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month13 days, 18 hours left to enroll

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question