SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2007
    Location
    London
    Posts
    161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Talking php unlink help please

    I want to delete some files in a directory that are older than 24hrs so i figured i could use the following but i'm not sure if this won't just delete everything so i thought i'd ask the pros first. the php code is aas follows

    PHP Code:
    function cleantmp() {
                    
    $seconds_old 86400;
                    
    $directory "/var/tmp";

                    if( !
    $dirhandle = @opendir($directory) )
                            return;

                    while( 
    false !== ($filename readdir($dirhandle)) ) {
                            if( 
    $filename != "." && $filename != ".." ) {
                                    
    $filename $directory"/"$filename;

                                    if( @
    filemtime($filename) < (time()-$seconds_old) )
                                            @
    unlink($filename);
                            }
                    }
            } 
    that php code i would execute via cron every now and then like so
    Code:
    /usr/local/bin/php -q /home/roinsonc494/delete.php
    Does this look sane enough to work? Wanna know what you think before i use it and potentially delete everything

  2. #2
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Looks safe to me.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #3
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Let the Linux command line do it instead
    Code:
    find /var/log -mtime +1 -type f -exec rm -rf {} \;
    This command will do a search in /var/log for all files that were last modified 1 or more days ago and executes a recursive forced (-rf) remove (rm).

    Can run that directly from cron without invoking PHP and adding more overhead to the server.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  4. #4
    SitePoint Zealot
    Join Date
    Jul 2007
    Location
    London
    Posts
    161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the replies trying the cron i'll see how it works out and get back


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •