note 100872 added to function.readdir
| From: | Qwerp at osu1 dot php dot net | Date: | Fri, 12 Nov 2010 06:42:06 +0000 |
| Subject: | note 100872 added to function.readdir | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-173964@lists.php.net to get a copy of this message | ||
Here's a handy function you can use to list the files in the directory you specify, their type
(dir or file) and whether they are hidden.
You could modify it to do other things too.
<?php
function listDirs($where){
echo "<table
border=\"1\"><tr><td><b>Name</b></td><td><b>Type</b></td>";
echo "<td><b>Invisible (Hidden)?</b></td></tr>";
$itemHandler=opendir($where);
$i=0;
while(($item=readdir($itemHandler)) !== false){
if(substr($item, 0, 1)!="."){
if(is_dir($item)){
echo
"<tr><td>$item</td><td>Directory</td><td>No</td></tr>";
}else{
echo
"<tr><td>$item</td><td>File</td><td>No</td></tr>";
}
$i++;
}else{
if(is_dir($item)){
echo
"<tr><td>$item</td><td>Directory</td><td>Yes</td></tr>";
}else{
echo
"<tr><td>$item</td><td>File</td><td>Yes</td></tr>";
}
$i++;
}
}
echo "</table>";
}
?>
Then call it like this.
<?php
listDirs(DIR);
?>
Example:
<?php
listDirs("/tests/directorylisting");
?>
You get a table like this.
Name Type Invisible (Hidden)?
. Directory Yes
.. Directory Yes
.DS_Store File Yes
.localized File Yes
index.php File No
OOOLS Directory No
QwerpWiki.php File No
----
Server IP: 69.147.83.197
Probable Submitter: 24.6.44.118
----
Manual Page -- http://www.php.net/manual/en/function.readdir.php
Edit -- https://master.php.net/note/edit/100872
Del: integrated -- https://master.php.net/note/delete/100872/integrated
Del: useless -- https://master.php.net/note/delete/100872/useless
Del: bad code -- https://master.php.net/note/delete/100872/bad+code
Del: spam -- https://master.php.net/note/delete/100872/spam
Del: non-english -- https://master.php.net/note/delete/100872/non-english
Del: in docs -- https://master.php.net/note/delete/100872/in+docs
Del: other reasons-- https://master.php.net/note/delete/100872
Reject -- https://master.php.net/note/reject/100872
Search -- https://master.php.net/manage/user-notes.php