0

How does one, in PHP, delete all files in a directory every 24 hours without deleting the directory itself? It has to be in PHP, it cannot be a Cron Job.

Can you please provide an explanation behind your code as well? I am still learning PHP.

Thank you!

Aaron Brewer
  • 3,567
  • 18
  • 48
  • 78

4 Answers4

3

There is no way to do this in PHP without using PHP. Sorry.

Joking, but if you wanted to do this you would need some sort of task scheduler (like cron).

That is to say that you could program your personal computer to send the request to the server every 24 hours, but you would either have to do it manually or schedule the task locally.

My point being, you need cron, but it doesn't need to be running on the same host as the PHP files.

David Houde
  • 4,835
  • 1
  • 20
  • 29
2

Without cron you'd have to add code like this to a commonly-requested page:

$scheduledclean = strtotime("midnight"); // 00:00:00 of the current day
$lastcleanfile = '/path/to/my/app/lastclean';
$lastcleantime = (file_exists($lastcleanfile)) ? filemtime($lastcleanfile) : 0;
$curtime = time();

if( ($curtime > $scheduledclean) && ($lastcleantime < $scheduledclean) ) {
    touch($lastcleanfile); //touch first to prevent multiple executions
    // file cleanup code here
}

On the first request to the page after midnight the cleanup will fire off, but the unlucky person that made the request will likely have a delay for their page to be served that is as long as the cleanup takes. You could mitigate this by running the cleanup as a backgrounded shell command like shell_exec('rm -rf /path/to/dir/* &');

Sammitch
  • 30,782
  • 7
  • 50
  • 77
1

I did something similar to this a long time ago. It's a terrible idea, but you can have a file which stores the last time your directory was cleared. Each time a user visits a relevant page, check this file in the PHP script (you could also check the modified time). If it is far enough into the past, update the file and run your delete script.

Downsides:

  • Not guaranteed to run every 24 hours (maybe you get no visitors one day)
  • Gives one user per day a longer wait
  • Ugly

As for deleting the files,

function delete_contents( $dirpath ) {
    $dir = opendir( $dirpath );
    if( $dir ) {
        while( ($s = readdir( $dir )) !== false ) {
            if( is_dir( $s ) ) {
                delete_contents( $s );
                rmdir( $s );
            } else {
                unlink( $s );
            }
        }
        closedir( $dir );
    }
}

BE VERY CAREFUL with that. On a crude server setup, delete_contents('/') will delete every file.

Dave
  • 44,275
  • 12
  • 65
  • 105
0
  1. Make a PHP script that removes all files in the directory, look for the functions readdir() and unlink() to remove the files.

  2. Set-up a Cron Job to run the script automatically each 24 hours. How you have to do this exactly depends on your host. There are also websites that you can use for this: http://www.google.nl/search?q=cronjobs+webservice

Good luck!

jeppeb
  • 528
  • 5
  • 12