I have a page the lists the filders/files in a current directory. Here is the code:
<?php
Function fncListDir( $dirname )
{
/ Get rid of those stupid filenames /
/ Was I sleepy when I did this? Alrighty then. /
if ( is_file( $DOCUMENT_ROOT.$REQUEST_URI ) )
{
$REQUEST_URI = ereg_replace( $mefile = strrchr( $REQUEST_URI, "/" ), "/", $REQUEST_URI );
};
/ Open Current Working Directory for reading. /
if( !$dirid = @opendir( $dirname.$recurse ) )
{
print " < - Unable to Open Directory";
return 1;
};
/ Read the contents of the directory one by one /
while ($entry = @readdir($dirid))
{
/ Do not list hidden files; begins with .
Why: Indexing . will cause infinite loop and eventually
a stack overfrow.
/
my regexp for ignorations. =🙂
- ignore all CVS directories and files
- ignore front page folders (they'd better not show up here!!!)
- ignore ".", "..", and hidden files
$ignore_regexp = "CVS|vti|.|index.php";
if (ereg($ignore_regexp ,$entry)) {
continue;
}
/ End File Exclusion /
/* Add this entry to the listing */
$dirEntries[] = $entry;
}
/* Sort listing alphabetically then reset to start */
sort( $dirEntries );
reset( $dirEntries );
/* HTML, Open an unordered list */
print "<ul>\n";
/* Walk through the current directory */
$i = 0;
while( $dirEntries[$i] )
{
/* Assign a short name */
$fileName = $dirname . "/" . $dirEntries[$i];
$fileNameShort = $dirEntries[$i];
if( is_dir( $fileName ) )
/* It is a directory structure */
{
/* HTML, Open directory list item */
print "\t<li class=\"folder\"><a href=\"$fileName\"><b><u>$fileNameShort</u></b>/</a>\n";
/* Recurse into subdirectory */
fncListDir( $fileName );
/* HTML, Close the directory list item */
print "</li>\n";
}
else
/* It is some type of file */
{
/* HTML, Create a list item entry. */
print "\t<li class=\"folderlink\"><a href=\"$fileName\">$fileNameShort</li>\n";
}
/* Increment the index else we loop forever, doh! */
$i ++;
}
/* HTML, Close the unordered list */
print "</ul>\n";
/ Wait, we're finished? /
};
$indexuri = explode("/", $REQUEST_URI);
print "<center><b><u>".urldecode($indexuri[count($indexuri)-2])."</u></b></center>\n";
fncListDir(".", $recurse, $REQUEST_URI, $DOCUMENT_ROOT);
?>
My problem is that if there are no files in a directory, I get the error:
Warning: Wrong datatype in sort() call in /documentation/vorfa/nav/directory.php on line 39
Warning: Variable passed to reset() is not an array or object in /documentation/vorfa/nav/directory.php on line 40
How can get this to not show the above error if there are no files in a directory?