Reputation: 75
I use the following function to get all the images from a dir, but instead of putting all the images in the $files
array, I would like to tweak it, so it only takes the images uploaded between 15:00:00 and 15:59:59 - today.
function get_files($images_dir,$exts = array('jpg')) {
$files = array();
if($handle = opendir($images_dir)) {
while(false !== ($file = readdir($handle))) {
$extension = strtolower(get_file_extension($file));
if($extension && in_array($extension,$exts)) {
$files[] = $file;
}
}
closedir($handle);
}
return $files;
}
But I can't figure out how to do it, and even less how to do it efficient since the folder has around 9000 images. Anyone can point me in the right direction?
Note: All the images are named like this "Snapshot-20110910-103242.jpg" with date-time.
Upvotes: 0
Views: 100
Reputation: 88697
function get_files ($images_dir, $exts = array('jpg'), $start = '15:00:00', $end = '15:59:59', $usemtime = FALSE) {
/**
* str $images_dir - Path to images directory
* arr $exts - Permitted file extensions
* str $start - Start time (H:i:s)
* str $end - End time (H:i:s)
* bool $usemtime - Use file modified time instead of file created time
*/
// Replace \ with / and strip trailing slashes so we can easily create a valid file path
$images_dir = rtrim(str_replace('\\','/',$images_dir),'/');
// Get start/end times
$starttime = strtotime($start);
$endtime = strtotime($end);
// Array to hold the results
$files = array();
// Open the directory and loop it
if ($handle = opendir($images_dir)) {
while (($file = readdir($handle)) !== FALSE) {
// Get this file's extension and created/modified time
$extension = strtolower(get_file_extension($file));
$filetime = ($usemtime) ? filemtime("$images_dir/$file") : filectime("$images_dir/$file");
// Check the file's extension and created or modified time
// I have removed the first $extension check because it is unnecessary)
if (in_array($extension,$exts) && $filetime >= $starttime && $filetime <= $endtime) {
$files[] = $file;
}
}
// Close the directory
closedir($handle);
}
// Return the list
return $files;
}
Upvotes: 2
Reputation: 2916
You could use the glob function to filter alot of the images. So, the glob call you'd use would be the following:
glob( $images_dir . '/Snapshot-20110912-15????.jpg' );
Upvotes: 1
Reputation: 599
You could get the file name, in this case Snapshot-20110910-103242.jpg and use substr to get the time on the file name like this:
$var = "Snapshot-20110910-103242.jpg";
$var = substr($var, -10,6);
Now that you have the time (103242), just use a if statement to check it the image was uploaded between 15:00:00 and 15:59:59
Upvotes: 0
Reputation: 15802
Use scandir to get a list of all files in the directory, then loop that array doing something like this to get matching filenames
function get_files($images_dir,$exts = array('jpg'))
{
$wantedFiles = array();
$scannedFiles = scandir($images_dir);
$todaysDate = date('Ymd');
foreach ($scannedFiles AS $file)
{
$filenameBits = explode('-', $file);
// Get the info we want
$numFilenameBits = count($filenameBits);
$fileData = explode('.', $filenameBits[$numFilenameBits - 1]);
if (in_array($fileData[1], $exts) && $filenameBits[$numFilenameBits - 2] == $todaysDate && $fileData[0] > 150000 && $fileData[0] < 155959)
$wantedFiles[] = $file;
}
return $wantedFiles;
}
Upvotes: 0
Reputation: 360772
This'll get the file's "last modified" timestamp, and figure out if it was last modified during the 3pm hour.
$stat = stat($images_dir . '/' . $file);
$mtime = $stat[9];
if (date('G', $stat[9]) == 15) {
... file was last modified sometime during 3-4pm
}
Upvotes: 0