1

the code down below delete all files inside a folder called " Images ". No complains everything works as it should, but is there a way to only delete the file which was created an hour ago or more then hour ago instead? Please show me how, I'm trying to learn by doing, please. Try to re-use the same code down below for better understanding and also to help users PHP programmer around the world

<?php
define('PATH', 'Images/');

function destroy($dir) {
    $mydir = opendir($dir);
    while(false !== ($file = readdir($mydir))) {
        if($file != "." && $file != "..") {
            chmod($dir.$file, 0777);
            if(is_dir($dir.$file)) {
                chdir('.');
                destroy($dir.$file.'/');
                rmdir($dir.$file) or DIE("couldn't delete $dir$file<br />");
            }
            else
                unlink($dir.$file) or DIE("couldn't delete $dir$file<br />");
        }
    }
    closedir($mydir);
}
destroy(PATH);
echo 'all done.';
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
user1508409
  • 109
  • 1
  • 3
  • 10
  • Look into using [`filemtime`](http://php.net/manual/en/function.filemtime.php) after making sure you're not in a directory to selectively delete the file or not – Aaron W. Jul 07 '12 at 13:41
  • @Aaron: `filemtime` returns the time the file was last modified, not the file creation time. – Jocelyn Jul 07 '12 at 13:42
  • Aaron, is there any change if you don't mind to shoe me example by using the same code i have posted? i'm going accept your answer in case it do what expected, please that would let me understand things even better and maybe help other new programmer instead of just keep it as dream – user1508409 Jul 07 '12 at 13:44
  • Jocelyn what is a better or right way? could you show it, please – user1508409 Jul 07 '12 at 13:45
  • @Jocelyn I read the question too quick, though it could still work if these files are just created and not modified after the fact since it would still return the right time. Also depends on the filesystem being used to get creation date. – Aaron W. Jul 07 '12 at 13:48
  • There is no creation time for Unix files in most Unix filesystems. – Federkun Jul 07 '12 at 13:57

3 Answers3

3

You can use filemtime() or filectime(). In addition, you can't use rmdir(), because you want to delete specific files.

What you have to do

  1. date("U",filectime($file) - return the last time your file was modified in Unix Epoch time.

  2. unlink($file) - deletes the specified file. You have to use it instead of rmdir().

  3. An if and while/for statement is needed:

    while($file)
    {
        if(date("U",filectime($file) <= time() - 3600)
        {
            unlink($file)
        }
    }
    

Then add it to your script:

function destroy($dir) {
$mydir = opendir($dir);
while($file = readdir($mydir)) {
    if($file != "." && $file != "..") {
        chmod($dir.$file, 0777);
        if(is_dir($dir.$file)) {
            chdir('.');
            while($dir.$file) {
                if(date("U",filectime($file) >= time() - 3600)
                {
                    unlink($dir.$file)
                }
            }

        }
        else
            unlink($dir.$file) or DIE("couldn't delete $dir$file<br />");
    }
}
closedir($mydir);
}

I hope it helps you.

Remember:

  • There is no way to get the file creation time in Unix/Php
  • The current if statement returns true if the file was last changed more or equal to one hour ago
Nadav S.
  • 2,429
  • 2
  • 24
  • 38
  • I'm using mac, but the web host is using Unix, could you implant that code inside the code i posted to see how it should be, please then i'm going pick you – user1508409 Jul 07 '12 at 14:09
  • Sure, give me 5 minutes. – Nadav S. Jul 07 '12 at 14:15
  • @user1508409 you may try the solution in my edited answer. – Nadav S. Jul 07 '12 at 14:25
  • I did, but it says : syntax error, unexpected '{' – user1508409 Jul 07 '12 at 14:28
  • Hey, hey hey! I'm here to help you with deleting files older than one hour ago, I'm not your personal assistant. Try to find yourself what's the current issue. And if I helped you with your **issue** that was in your **question**, upvote or accept it. I'm almost sure that you have to check the previous script, the script in my answer contains the exact amount of `{`. – Nadav S. Jul 07 '12 at 14:33
  • Sure, but the code didn't work... so it didn't really help.. i jsut told you what it says, but you... – user1508409 Jul 07 '12 at 14:36
  • @user1508409 Maybe you want to continue the discussion in chat? – Nadav S. Jul 07 '12 at 14:40
  • I want, but stackoverflow dont allowed me .. – user1508409 Jul 07 '12 at 14:48
  • OK, I found this very helpful - but one of the lines is definitely wrong and should read: if(date("U",filectime($dir)) <= ((time())-60)){ Please note - I have put the { opener on the same line - adjust to suit.... – chris May 15 '16 at 11:02
  • hey @chris , why 60? remember we count time in seconds and not minutes - so 60*60=3600. regarding the operator - you're right! – Nadav S. May 17 '16 at 04:56
3

I would not use php for this but a simple shell script in combination with a cron job (on linux at least).

If you are on linux, the script would look something like:

#! /bin/sh
find /path/to/Images/ -mmin +60 -exec rm -f {} \;     // modification date, not creation date...

And I would use cron to run it automatically once every hour.

For completion, the cron entry would look like:

0 * * * * /path/to/shell/script.sh    // run script every whole hour

Not a direct answer to the question, but too long for a comment and perhaps a possible solution as all answers in the duplicate question are focused on php as well...

jeroen
  • 91,079
  • 21
  • 114
  • 132
0

In Unix, it is impossible to retrieve the creation time, only the last modified date using filectime

However, if you're on a windows system, it's exactly the reverse. Using the same method filectime, you will retrieve the creation time and not the last changed time.

Hyunmin Kim
  • 931
  • 2
  • 8
  • 18
  • Hyunmin Kim, implant filectime in the code i have posted and i'm going accept your answer as reward, please you look like expert – user1508409 Jul 07 '12 at 13:59
  • This will only work on windows, but the basic idea is this: convert time to DateTime object. Compare with current time using DateTime::diff() (http://www.php.net/manual/en/datetime.diff.php) Make a conditional statement saying if it's more than an hour past creation time, delete file – Hyunmin Kim Jul 07 '12 at 14:04