note 66466 added to function.readdir
| From: | repleyatfreemaildotit at osu1 dot php dot net | Date: | Tue, 23 May 2006 14:27:52 +0000 |
| Subject: | note 66466 added to function.readdir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-111885@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 213.92.11.19
Probable Submitter: 84.222.147.252
----
X-Spam-Status: No, hits=3.3 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER,
HTML_MESSAGE autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.readdir.php
Edit -- http://master.php.net/note/edit/66466
Del: integrated -- http://master.php.net/note/delete/66466/integrated
Del: useless -- http://master.php.net/note/delete/66466/useless
Del: bad code -- http://master.php.net/note/delete/66466/bad+code
Del: spam -- http://master.php.net/note/delete/66466/spam
Del: non-english -- http://master.php.net/note/delete/66466/non-english
Del: in docs -- http://master.php.net/note/delete/66466/in+docs
Del: other reasons-- http://master.php.net/note/delete/66466
Reject -- http://master.php.net/note/reject/66466
Search -- http://master.php.net/manage/user-notes.php