note 66466 deleted from function.readdir by tularis
| From: | tularis@php.net | Date: | Sun, 13 May 2007 19:00:41 +0000 |
| Subject: | note 66466 deleted from function.readdir by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-126509@lists.php.net to get a copy of this message | ||
Note Submitter: repley at freemail dot it
----
Upgrade of davidstummer's listImages function: now with order by and sorting direction (like
SQL ORDER BY clause).
<?php
//listImages(string path, string sort-by name | width | height | size, int sorting order flags
SORT_ASC | SORT_DESC)
function listImages($dirname, $sortby, $sortdir) {
$ext = array("jpg", "png", "jpeg", "gif");
$files = array();
if($handle = opendir($dirname)) {
while(false !== ($file = readdir($handle))){
for($i=0;$i<sizeof($ext);$i++){
if(stristr($file, ".".$ext[$i])){ //NOT case sensitive: OK with JpeG, JPG,
ecc.
$imgdata = getimagesize($dirname . "/" . $file);
$filesize = filesize($dirname . "/" . $file);
//Some formats may contain no image or may contain multiple images.
//In these cases, getimagesize() might not be able to properly determine the
image size.
//getimagesize() will return zero for width and height in these cases.
if($imgdata[0] && $imgdata[1] && $filesize){
$files[] = array(
"name" => $file,
"width" => $imgdata[0],
"height" => $imgdata[1],
"size" => $filesize
);
}
}
}
}
closedir($handle);
}
//obtain an array of columns
foreach ($files as $key => $row) {
$name[$key] = $row['name'];
$width[$key] = $row['width'];
$height[$key] = $row['height'];
$size[$key] = $row['size'];
}
return array_multisort($$sortby, $sortdir, $files) ? $files : false;
}
//end listImages
//start test
$sortby = "width"; // sort-by column; accepted values: name OR width OR height OR size
$path = "path"; // path to folder
$sortdir = SORT_ASC; // sorting order flags; accepted values: SORT_ASC or SORT_DESC
$files = listImages($path, $sortby, $sortdir);
foreach ($files as $file){
echo 'name = <strong>' . $file['name'] . '</strong> width =
<strong>' . $file['width'] . '</strong> height =
<strong>' . $file['height'] . '</strong> size = <strong>'
. $file['size'] . '</strong><br />';
}
//end test
?>
Repley.