note 71631 deleted from function.readdir by tularis
| From: | tularis@php.net | Date: | Sun, 13 May 2007 18:59:02 +0000 |
| Subject: | note 71631 deleted from function.readdir by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-126499@lists.php.net to get a copy of this message | ||
Note Submitter: mike at mihalism dot com
----
This small snipet will list and sort files from a directory ! This script also will only show file
types that you allow. To change the files types edit the $file_types array.
<?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)){
$file_count++;
echo '<a href="'.$path.$file.'">'.$file.'</a>
<br/>';
}
}
?>