Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New Coder
    Join Date
    May 2005
    Location
    Utah
    Posts
    65
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Delete old files automatically

    I have some code that will bring up images in a folder. How can I have it search for images and delete all images that are older than, say 6 months?
    PHP Code:
    $handle=opendir("./content/uploaded/");  
    $aFiles = array();  
                      {  
    while ((
    $file readdir($handle))!==false)  
       {   
     
    $aFiles[$file] = filectime('./content/uploaded/' $file);  
    }  
    asort($aFiles);    
    foreach(
    $aFiles as $key => $value){ 
        echo( 
    $key " - " date("F d Y ."$value). "<a href='content/uploaded/".$key."' target='blank'><img src=./content/uploaded/" $key " width=300></a><br><br><hr>");   
        }  
        }  
        
    closedir($handle); 
    Thanks!

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    First thing you'd do is determine what the age of six months ago is. That's a simple strtotime or datetime that can be done.
    PHP Code:
    $iSixMonthsAgo strtotime('-6 month'); 
    Then you simply compare. If you don't need to show what is deleted, you can do it in the while loop.
    PHP Code:
    $sPath './content/uploaded';
    //. . .
    $sPath rtrim($sPath'/\\');
    while (
    false !== ($file readdir($handle)))
    {
        if (
    filectime($sPath DIRECTORY_SEPARATOR $file) <= $iSixMonthsAgo)
        {
            
    unlink($sPath DIRECTORY_SEPARATOR $file);
        }
        else
        {
            
    $aFiles[$file] = filectime($sPath DIRECTORY_SEPARATOR $file);
        }

    Like that. Test it out in a sandbox since you are deleting files.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    New Coder
    Join Date
    May 2005
    Location
    Utah
    Posts
    65
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Thanks, it worked....kinda. I deleted all of the files over 6 months but it also posts some weirld stuff. And I can't get it to display from newest to oldest anymore.
    PHP Code:
    {
    $iSixMonthsAgo strtotime('-6 month'); 
            
    $handle=opendir("./content/uploaded/");
             
    $sPath "content/uploaded/";
      
    $sPath rtrim($sPath'/\\');  
            
    $aFiles = array();  
                  {  
                    while (
    false !== ($file readdir($handle)))
                        {  if (
    filectime($sPath DIRECTORY_SEPARATOR $file) <= $iSixMonthsAgo)
                                    {    
    unlink($sPath DIRECTORY_SEPARATOR $file);
                                 }
                          else{  
                        
                              
    asort($aFiles[$file] = filectime($sPath DIRECTORY_SEPARATOR $file));
        
                            foreach(
    $aFiles as $key => $value){ 
                            echo( 
    $key " - " date("F d Y ."$value). "<a href='content/uploaded/".$key."' target='blank'><img src=./content/uploaded/" $key " width=300></a><br><br><hr>");   
                         }  
                        }  
                   }  
              
        
    closedir($handle);  
              }
        } 
    The issue is at http://www.localheromx.com/flex-grap...magecheck2.php
    Thanks

  • #4
    New Coder
    Join Date
    Apr 2010
    Posts
    55
    Thanks
    0
    Thanked 4 Times in 4 Posts
    I haven't checked the code in detail, just few thoughts.

    1. Your coding style is poor and you need to do something urgently about it. Indentation is not something you do before going live, one should never write a line of code without proper indentation.

    2. First braces before while loop looks unnecessary.

    3. You re sorting and printing inside the while loop. Terrible. Let the deletion process complete, come out the loop and sort and then print. This is the reason for long execution time and multiple occurance of each files.

    4. You are printing ./ and ../ which should be omitted conditionally and should not be added to the array.

    5. Debugging is a lot easier if you print the array using print_r instead of echoing the complete HTML with img tag to the browser. Your debugging techniques are poor.


    Honestly, you can't ask for help and wait for hours with such easy tasks. Matter of getting your coding style, logic and debugging techniques right.

    Sorry if this sounds harsh, but I think it will be worse if I simply give the solution and not allow you to improve. Changes are easy when you are a beginner.
    Last edited by alemcherry; 02-26-2013 at 10:43 AM.
    Hosting Reviews and Discounts: Bluehost Coupon and Hostmonster Coupon

  • Users who have thanked alemcherry for this post:

    Local Hero (02-26-2013)

  • #5
    New Coder
    Join Date
    May 2005
    Location
    Utah
    Posts
    65
    Thanks
    6
    Thanked 0 Times in 0 Posts
    I really do appreciate the advice. I'll go over it and come back. Thanks!

  • #6
    New Coder
    Join Date
    May 2005
    Location
    Utah
    Posts
    65
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Ok, I've gone through what you're said and cleaned it up a bit. Irealize that I'm still running the 2nd while loop inside of the 1st, but I get a blank page when I seperate them.
    The ./ and ../ are gone.
    I changed to the print_r, but I'm not seeing something. I have been looking over the code all morning and I can't figure out what's the issue?
    PHP Code:
    else{
    $handle=opendir("./content/uploaded/");
    $sPath "content/uploaded/";
    $sPath rtrim($sPath'/\\');  
    $aFiles = array();  
                    
        while (
    false !== ($file readdir($handle)))
                {  if (
    filectime($sPath DIRECTORY_SEPARATOR $file) <= $iSixMonthsAgo)
                    {    
    unlink($sPath DIRECTORY_SEPARATOR $file);
                    }
        while (
    false !== ($file readdir($handle)))
                    {if((
    $file != ".") and ( $file != ".."))
                      
                
    asort($aFiles[$file] = filectime($sPath DIRECTORY_SEPARATOR $file));
                    foreach(
    $aFiles as $key => $value)
                        { 
                        
    print_r$key " - " date("F d Y ."$value). "<a href='content/uploaded/".$key."' target='blank'><img src=./content/uploaded/" $key " width=300></a><br><br><hr>"); 
                        }  
          
                    }
          
    closedir($handle);
                }
                
        } 
    Last edited by Local Hero; 02-26-2013 at 08:27 PM.

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    This still isn't correct. For each iteration of a file you iterate over every existing file. This will create a nice parabolic magnitude (or would, but it will never reach this condition since its not rewinded).

    PHP Code:
    while (false !== ($file readdir($handle)))
    {
        if (
    filectime($sPath DIRECTORY_SEPARATOR $file) <= $iSixMonthsAgo)
        {
            
    unlink($sPath DIRECTORY_SEPARATOR $file);
        }
        else
        {
            
    $aFiles[$file] = filectime($sPath DIRECTORY_SEPARATOR $file);
        }

    asort($aFiles);
    foreach (
    $aFiles AS $fileName => $createTime)
    {
        
    printf('%s (created on %s)' PHP_EOL$fileNamedate('F d Y .'$createTime));

    The first loop unlinks the old files and appends the existing ones as associative offsets with a value of the creation time. The foreach then iterates the array to display them.

    Things like this will not work:
    PHP Code:
        while (false !== ($file readdir($handle)))
                {  if (
    filectime($sPath DIRECTORY_SEPARATOR $file) <= $iSixMonthsAgo)
                    {    
    unlink($sPath DIRECTORY_SEPARATOR $file);
                    }
        while (
    false !== ($file readdir($handle))) 
    readdir increments the pointer until it reaches end of file which returns false which now breaks the loop. The next instruction asks to readdir again, which will return false automatically since its already sitting at the end. You would need a rewinddir() call to place it back at the beginning.

    Things like this are definitely not doable:
    PHP Code:
                asort($aFiles[$file] = filectime($sPath DIRECTORY_SEPARATOR $file)); 
    asort (actually many sort's), have the following signature: boolean asort(array &, [int]) which is by reference. The array needs to be provided as the argument, not an assignment expression.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    Local Hero (02-26-2013)

  • #8
    New Coder
    Join Date
    May 2005
    Location
    Utah
    Posts
    65
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Wow, that worked nicely! I wouldn't have stumbled on that. I'm not quite sure what PHP_EOL does. I looked it up and I know waht it means but what does it do for me?

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    PHP_EOL is system linefeed constant. This way you don't concern yourself with \n or \r\n.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

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