Title pretty much says it all. I've been using this to sort image files by their timestamps on wamp and it works fine:
PHP Code:
<?php
function returnimages(){
$dirname = isset($_GET['dir'])? $_GET['dir'] : '.';
chdir($dirname);
$pattern = '#\.(jpg|jpeg|png|gif|bmp)$#i';
$files = array();
if($handle = opendir('.')){
while(false !== ($file = readdir($handle))){
if(preg_match($pattern, $file)){
$filedate = filemtime($file);
$files[$file] = $filedate;
}
}
closedir($handle);
}
arsort($files, SORT_NUMERIC);
$files = array_keys($files);
echo " ['$dirname/" . implode("'],\n ['$dirname/", $files) . "']\n";
}
returnimages();
?>
Notice particularly:
Code:
$files[$file] = $filedate;
I do it that way because no two files in the same folder will have the same filename, but they might have the same timestamp. If they do and I make the stamp the key, one entry will overwrite the other.
Anyways, I then have keys like some.jpg, here's a print_r of a typical array this produces (before the arsort):
Array
(
[image1.jpg] => 1150840114
[image2.jpg] => 1192236872
[image3.jpg] => 1192236862
[image4.jpg] => 1150840110
[image5.jpg] => 1192236848
[thumb_image1.jpg] => 1190861632
[thumb_image2.jpg] => 1190384470
[thumb_image3.jpg] => 1190384470
[thumb_image4.jpg] => 1190384470
[thumb_image5.jpg] => 1190384472
)
I was a little worried about the . - it's a big no-no for object keys in javascript unless you stringify the key by quoting it. But PHP seems fine with it. Is there anything that could be in a PHP fetched filename that could result in an invalid key here?
Bookmarks