SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Nov 2004
    Location
    Mid-West
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Delete upload images if session time has been longer than 45 Minutes

    This problem has been <=SOLVED=>. The code below is correct.
    If there is a easier way let me know.

    Can someone point me in the right direction on how I would delete files from a directory once the session time has not been updated in 45 minutes. This script would eventually be run from a cron. The part that I'm having problems with is the preg_match and then the delete.


    Session directroy example: /usr/sses_4a2ae4d68edd9294db450a318bda6598

    Image directory example:
    /photos/temp_photos/1_image_4a2ae4d68edd9294db450a318bda6598.jpg
    /photos/temp_photos/2_image_4a2ae4d68edd9294db450a318bda6598.jpg
    /photos/temp_photos/3_image_4a2ae4d68edd9294db450a318bda6598.jpg

    PHP Code:
    #directory to scan
    $image_dir $_SERVER['DOCUMENT_ROOT'] . "/photos/temp_photos";
    $session_dir $_SERVER['DOCUMENT_ROOT'] . "/user";

    #time in seconds
    $time "2700";// 45 Minutes

    # Open Directory to be read
    if($session opendir($session_dir))
       {
          
    #Loop through Files in the Directory
          
    while (false !== ($userSession readdir($session)))
                {
                   
    #skip "." and ".."
                   
    if($userSession != "." && $userSession != "..")
                     {                        
                        
    #get current time
                        
    $currentTime date(U);

                        
    #get file times
                        
    $fileTime date(filemtime("$session_dir/$userSession"));
                    
                        
    #subtract to come up with the difference
                        
    $remainder $currentTime $fileTime;
                          
                        
    # removes the "sess_" from the beginning of the session
                        
    $userSession substr($userSession5);

                        
    #Time test, if sesssion is older than 45 minutes continue
                        
    if ($remainder $time)
                           {
                              
    $session_array [] = $userSession;
                           }
    //end if
                     
    }//end if
            
    }//end while
       
    }//end if
       #print_r ($session_array);

    if(!empty($session_array))
      {
         
    # Open Directory to be read
         
    if($image opendir($image_dir))
           {
              
    #Loop through Files in the Directory
              
    while(false !== ($userImage readdir($image)))
                   {
                      
    #skip "." and ".."
                      
    if($userImage != "." && $userImage != "..")
                        {                        
                           
    # Create image array of images in the temp directory
                           
    $image_array [] = $userImage;
                        }
    //end if
                   
    }//end while
           
    }//end if
           #print_r ($image_array);
         #Compare session name to file name to find match
         
    if(!empty($image_array))
           {
              foreach(
    $session_array as $session_value)
                     {
    #print "$value1";
                      
    $a_array preg_grep("/".$session_value."/"$image_array);
                      
    #print_r ($a_array);
                      
    foreach ($a_array as $value)
                              {
                                 print 
    "$value ";
                                 
    unlink("$image_dir/$value");
                              }
                     }
           }

    Last edited by SunGod; Nov 29, 2005 at 16:42. Reason: <=SOLVED=>

  2. #2
    SitePoint Member
    Join Date
    Nov 2004
    Location
    Mid-West
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    <=SOLVED=>

    Problem has been solved. The thread code has been updated.
    If there is an easier way let know.

    SG


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
  •