can you write me a simple php script?

Discussion in 'PHP' started by bluemouse2, Mar 15, 2006.

  1. #1
    Hi!
    I need a simple php script (a few lines of code probably) which will delete all the files in a directory that are older than x hours.

    for ex: to delete all the files in a directory that are older than 12 hours

    In exchange I'll give you a featured link in my webmastertag.com PR3 directory

    Thanks!
     
    bluemouse2, Mar 15, 2006 IP
  2. chengfu

    chengfu Well-Known Member

    Messages:
    113
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    108
    #2
    Are you sure you need PHP? If you are using a linux server the find command might be your friend:

    
    find /dir -cmin +720 -exec rm {} \;
    
    Code (markup):
     
    chengfu, Mar 15, 2006 IP
  3. chengfu

    chengfu Well-Known Member

    Messages:
    113
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    108
    #3
    In PHP it could be solved like this:

    
    <?php
    $dir = "/tmp/"; # directory to check
    $dh = opendir($dir);
    while(false !== ($file = readdir($dh))) {
    	$file = $dir.$file;
    	if (filetype($file) != "file") continue; # Only files
    	if (filectime($file) < (time() - (12 * 3600))) { # 12 hours old
    		unlink($file); # Delete file
    	}
    }
    closedir($dh);
    ?>
    
    Code (markup):
    But if your server is running linux I would always prefer the solution with "find". PHP is oversized for just deleting some files.
     
    chengfu, Mar 15, 2006 IP
  4. sketch

    sketch Well-Known Member

    Messages:
    898
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    148
    #4
    There could be many reasons why he wants to use PHP... CRON job, he doesn't have shell access, etc.

    Just one thing to note on chengfu's code... filectime($file) determines the file's CREATION time... if you want to look at the file's last modified time, use filemtime($file).
     
    sketch, Mar 15, 2006 IP
    chengfu likes this.
  5. chengfu

    chengfu Well-Known Member

    Messages:
    113
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    108
    #5
    Ups, of course sketch is right. I was confusing filectime as "file change time" ;)
     
    chengfu, Mar 15, 2006 IP
  6. sketch

    sketch Well-Known Member

    Messages:
    898
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    148
    #6
    Just giving bluemouse an option... maybe he DOES want it by creation time, he didn't specify :)
     
    sketch, Mar 15, 2006 IP
  7. bluemouse2

    bluemouse2 Well-Known Member

    Messages:
    4,055
    Likes Received:
    130
    Best Answers:
    0
    Trophy Points:
    185
    #7
    thanks you!
    just one more thing: how can I exclude a file to not be deleted? let's say test.html - I want this to remain on the server
    I intend to run this script via a cron or everytime another php file is accessed (via php include)

    chengfu and sketch - please each one add your site to webmastertag.com and let me know so I can approve it (you'll get a featured link)
     
    bluemouse2, Mar 15, 2006 IP
  8. sketch

    sketch Well-Known Member

    Messages:
    898
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    148
    #8
    Off the top of my head, if you only want to test for the one file (i.e. test.html), the last IF statement should be if (($file != "test.html") AND filectime($file) < (time() - (12 * 3600))).

    If you want to test for a list of files, create an array with those files, then the last IF statement should be if (!in_array($donotdeletethese) AND filectime($file) < (time() - (12 * 3600))) {.

    I didn't mean to hijack chengfu's code :$ ... but I've added my BabeAudit.com link to the Entertainment section! Thanks!
     
    sketch, Mar 15, 2006 IP
  9. chengfu

    chengfu Well-Known Member

    Messages:
    113
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    108
    #9
    Perhaps you already noticed - I dont like doing sysadmin stuff using php :)
    On a linux system tmpreaper might be an option. It is specifically for purging old files from directories and excluding certain files.

    Thanks for the link offer bluemouse2. Unfortunately my choice of english sites is limited, so I added my blog "chengfu.net" to your web logs category.
     
    chengfu, Mar 16, 2006 IP
  10. bluemouse2

    bluemouse2 Well-Known Member

    Messages:
    4,055
    Likes Received:
    130
    Best Answers:
    0
    Trophy Points:
    185
    #10
    thanks!
    both sites accepted as featured links ;)

    I'll try the script later and let you know if I need something else.
     
    bluemouse2, Mar 16, 2006 IP
  11. bluemouse2

    bluemouse2 Well-Known Member

    Messages:
    4,055
    Likes Received:
    130
    Best Answers:
    0
    Trophy Points:
    185
    #11
    the script works ok
    however when I try to exclude a file to be deleted I get errors (and they are still all deleted)
    do you have another idea how to exclude a specific file?
    thanks!
     
    bluemouse2, Mar 16, 2006 IP
  12. chengfu

    chengfu Well-Known Member

    Messages:
    113
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    108
    #12
    Could you paste the complete script you are using now? Or hjow you are defining the exclude files?
    If everything is correct sketch's solution should work fine.
     
    chengfu, Mar 16, 2006 IP
  13. bluemouse2

    bluemouse2 Well-Known Member

    Messages:
    4,055
    Likes Received:
    130
    Best Answers:
    0
    Trophy Points:
    185
    #13
    If you want to test for a list of files, create an array with those files, then the last IF statement should be if (!in_array($donotdeletethese) AND filectime($file) < (time() - (12 * 3600))) {.

    how do I create an array with those files?
     
    bluemouse2, Mar 16, 2006 IP
  14. sketch

    sketch Well-Known Member

    Messages:
    898
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    148
    #14
    Like this:

    $donotdeletethese = array("file1.html","file2.html","someimg.jpg");

    Make sure to put the filenames in quotes and separate each one with a comma.
     
    sketch, Mar 16, 2006 IP
  15. bluemouse2

    bluemouse2 Well-Known Member

    Messages:
    4,055
    Likes Received:
    130
    Best Answers:
    0
    Trophy Points:
    185
    #15
    I get this error:
    Wrong parameter count for in_array()
    in line:
    if (!in_array($donotdeletethese) AND filectime($file) < (time() - (1 * 3600))) {

     
    bluemouse2, Mar 16, 2006 IP
  16. sketch

    sketch Well-Known Member

    Messages:
    898
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    148
    #16
    That's because I'm a very dumb programmer :D ... Change

    $file = $dir.$file

    to

    $fullpath = $dir.$file

    then add $file, (with comma) after "in_array(", should work (it did here :p )
     
    sketch, Mar 16, 2006 IP
  17. bluemouse2

    bluemouse2 Well-Known Member

    Messages:
    4,055
    Likes Received:
    130
    Best Answers:
    0
    Trophy Points:
    185
    #17
    related to the last line:

    then add $file, (with comma) after "in_array(", should work (it did here )

    can you please be more specific?
    thanks :)
     
    bluemouse2, Mar 16, 2006 IP
  18. chengfu

    chengfu Well-Known Member

    Messages:
    113
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    108
    #18
    This should be the complete code for sketch's proposal:

    
    <?php
    $dir = "/dir/"; # directory to check
    $dh = opendir($dir);
    while(false !== ($file = readdir($dh))) {
    $donotdeletethese = array("test.html","test.php");
    $fullpath = $dir.$file;
    if (filetype($fullpath) != "file") continue; # Only files
    if (!in_array($file, $donotdeletethese) AND filectime($fullpath) < (time() - (1 * 3600))) {
    unlink($fullpath); # Delete file
    
    }
    }
    closedir($dh);
    ?>
    
    Code (markup):
     
    chengfu, Mar 17, 2006 IP
  19. bluemouse2

    bluemouse2 Well-Known Member

    Messages:
    4,055
    Likes Received:
    130
    Best Answers:
    0
    Trophy Points:
    185
    #19
    thank you! it works now ;)
     
    bluemouse2, Mar 17, 2006 IP