Page 1 of 1

help? very new.

Posted: Sat Nov 28, 2009 11:24 pm
by regularguy
Hello all,
I wonder if some can help a vnewbie.
I have an image gallery ( photo club members ) but I only want to show only the latest 24 images in the gallery folder, and delete the older files. Perhaps, I can run this by crontab or just clicking a php file??

Thanks!!!!

J.

Re: help? very new.

Posted: Sun Nov 29, 2009 12:27 pm
by AlanG
It's just a matter of looping through the directory and determining which files are older and removing them.
  • The code below loops through the directory and builds an array of data (key = filename, value = last modified time)
  • It then sorts and reverses the array (putting the newest files to the front)
  • Finally it compares the list of files to keep with the list of all files, removing files not in the $keepFiles array
There are 2 configuration options at the top you can change. :)

Code: Select all

<?php
// Config options
$imageDir = './images/';
$maxFiles = 24;
 
$files = array();
    
$dir = dir($imageDir);
 
while (($file = $dir->read()) !== false) {
    if(substr($file, 0, 1) != '.') { // Ignores current and parent directory paths and also hidden files (Unix)
        $files[$file] = filemtime($imageDir . $file);
    }
}
 
$dir->close();
 
$numFiles = count($files);
 
if($numFiles > $maxFiles) { // We only have a need to sort the array and filter it if there are more than the maximum allowed files in the directory
    arsort($files, SORT_NUMERIC); // Sorts the $files array, placing the newest files to the front
    
    $keepFiles = array_keys(array_slice($files, 0, $maxFiles));
    
    // Compares files in the $files array with the $keepFiles array, and deletes files not in the $keepFiles array
    foreach($files as $filename => $modifiedTime) {
        if(!in_array($filename, $keepFiles, true)) {
            unlink($imageDir . $filename);
        }
    }
}
?>

Re: help? very new.

Posted: Thu Dec 03, 2009 4:24 pm
by regularguy
First, Thank You! for your time.
Second, have a nice holidays!

after running the file above, I have this messsage:

"Parse error: syntax error, unexpected T_DNUMBER in /homepages/43/d163462424/htdocs.... on line 3"

actually, I Only want to delete all the ".jpg" and ".jpeg" files.

Again, Thank you.

Re: help? very new.

Posted: Thu Dec 03, 2009 5:39 pm
by AlanG
regularguy wrote:First, Thank You! for your time.
Second, have a nice holidays!

after running the file above, I have this messsage:

"Parse error: syntax error, unexpected T_DNUMBER in /homepages/43/d163462424/htdocs.... on line 3"

actually, I Only want to delete all the ".jpg" and ".jpeg" files.

Again, Thank you.
That error isn't directly related to the code I wrote. What code exactly is causing the problem? I tested my code and it works as expected.

Here's updated code to allow you to choose specific file extensions to remove.

Code: Select all

 
<?php
// Config options
$fileDir = './images/';
$maxFiles = 24; // The number of files (with extensions in the $removeExts array) that may be stored in the $fileDir at any one time.
$removeExts = array('jpg', 'jpeg'); // Extensions that will be removed if the $maxFiles limit is exceeded.
 
// ---- Don't modify the below code unless you know what you are doing ----
 
$files = array();
   
$dir = dir($fileDir);
 
while (($file = $dir->read()) !== false) {
    if(substr($file, 0, 1) != '.') { // Ignores current and parent directory paths and also hidden files (Unix)
        $ext = end(explode('.', $file));
      
        if(in_array($ext, $removeExts)) {
            $files[$file] = filemtime($imageDir . $file);
        }
    }
}
 
$dir->close();
 
$numFiles = count($files);
 
if($numFiles > $maxFiles) { // We only have a need to sort the array and filter it if there are more than the maximum allowed files in the directory
    arsort($files, SORT_NUMERIC); // Sorts the $files array, placing the newest files to the front
   
    $keepFiles = array_keys(array_slice($files, 0, $maxFiles));
   
    // Compares files in the $files array with the $keepFiles array, and deletes files not in the $keepFiles array
    foreach($files as $filename => $modifiedTime) {
        if(!in_array($filename, $keepFiles, true)) {
            unlink($fileDir . $filename);
        }
    }
}
?>
 
You should make sure that the $fileDir has sufficient priviledges to allow you to remove the files (allow the write permission).
To use this file in your own scripts, you should simply include it. e.g.

Code: Select all

include_once('/path/to/file/autoremove_images.php');

Re: help? very new.

Posted: Sat Dec 05, 2009 11:51 pm
by regularguy
Yes! it works.
Not just it works, I'm also learning! thank you.

Re: help? very new.

Posted: Sat Dec 05, 2009 11:56 pm
by AlanG
No problem. Your welcome (always nice to get a thank you) :)