note 72225 added to function.readdir
| From: | danls at hotmail dot com | Date: | Mon, 08 Jan 2007 16:30:16 +0000 |
| Subject: | note 72225 added to function.readdir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-121452@lists.php.net to get a copy of this message | ||
In response to mike at mihalism dot com:
Thanks for your snippet, it was really helpful, however there is a small error. When you search for
allowed extensions using array_search, the first extension you set in the $file_types variable will
not be printed (since array_search will return 0, the first key in the array). This can be easily
solved by checking that the return value from array search is not identical to false. Here is your
code again, with the correction:
<?php
function file_type($file){
$path_chunks = explode("/", $file);
$thefile = $path_chunks[count($path_chunks) - 1];
$dotpos = strrpos($thefile, ".");
return strtolower(substr($thefile, $dotpos + 1));
}
$path = "files/";
$file_types = array('jpeg', 'jpg', 'ico', 'png',
'gif', 'bmp', 'html');
$p = opendir($path);
while (false !== ($filename = readdir($p))) {
$files[] = $filename;
}
sort($files);
foreach ($files as $file) {
$extension = file_type($file);
if($file != '.' && $file != '..' &&
array_search($extension, $file_types) !== false){
$file_count++;
echo '<a
href="'.$path.$file.'">'.$file.'</a> <br/>';
}
}
?>
----
Server IP: 64.71.164.2
Probable Submitter: 209.200.139.10
----
Manual Page -- http://www.php.net/manual/en/function.readdir.php
Edit -- https://master.php.net/note/edit/72225
Del: integrated -- https://master.php.net/note/delete/72225/integrated
Del: useless -- https://master.php.net/note/delete/72225/useless
Del: bad code -- https://master.php.net/note/delete/72225/bad+code
Del: spam -- https://master.php.net/note/delete/72225/spam
Del: non-english -- https://master.php.net/note/delete/72225/non-english
Del: in docs -- https://master.php.net/note/delete/72225/in+docs
Del: other reasons-- https://master.php.net/note/delete/72225
Reject -- https://master.php.net/note/reject/72225
Search -- https://master.php.net/manage/user-notes.php