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.
help? very new.
Moderator: General Moderators
Re: help? very new.
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
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);
}
}
}
?>-
regularguy
- Forum Newbie
- Posts: 6
- Joined: Sat Nov 28, 2009 11:13 pm
Re: help? very new.
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.
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.
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.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.
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);
}
}
}
?>
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');-
regularguy
- Forum Newbie
- Posts: 6
- Joined: Sat Nov 28, 2009 11:13 pm
Re: help? very new.
Yes! it works.
Not just it works, I'm also learning! thank you.
Not just it works, I'm also learning! thank you.
Re: help? very new.
No problem. Your welcome (always nice to get a thank you) 