note 72225 deleted from function.readdir by tularis
| From: | tularis@php.net | Date: | Sun, 13 May 2007 18:58:52 +0000 |
| Subject: | note 72225 deleted from function.readdir by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-126497@lists.php.net to get a copy of this message | ||
Note Submitter: danls at hotmail dot com
----
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/>';
}
}
?>