|
-
Sep 23rd, 2006, 09:16 PM
#1
Thread Starter
Member
Directory Listing
Can anybody tell me how to list the contents of a directory??
Not all files but the files of a specified file type.
I ve managed to display all files using readdir, but still dont know how to filter files of a particular type.
Hope you guys can help me
Thanx in adwance
-
Sep 24th, 2006, 07:15 PM
#2
Re: Directory Listing
this will grab any files in $dir with a filetype listed in $filetypes. make sure you add a "|" to the end of each filetype, including the last. wasn't sure if you were only looking to display one specific filetype, or multiple, so i did it this way.
PHP Code:
<pre>
<?
//the directory to look in
$dir = "./";
//this is a list of the valid filetypes, seperated by a "|"
//make sure the last item also has a "|" at the end!
$filetypes = "html|zip|";
//start reading the directory
if(is_dir($dir)){
if($opendir = opendir($dir)){
while(($file = readdir($opendir)) !== false){
//find the last occurence of a period in the filename
$typepos = strrpos($file, ".");
//check if the typepos was returned
// --> if not, it does not have a filetype, so we ignore it
if($typepos > 0 && $file != ".."){
//isolate the filetype
$type = substr($file, $typepos + 1, strlen($file) - $typepos);
//check if the filetype is something we want
if(strpos($filetypes, $type . "|") !== false)
echo $file . " - " . strtoupper($type) . " file\n";
}
}
closedir($opendir);
}
}
?>
</pre>
hope it helps you out.
-
Sep 25th, 2006, 07:25 AM
#3
Thread Starter
Member
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|