is_dir is not correctly recognizing file vs folder
Posted: Sun Apr 05, 2009 6:10 pm
Hi all, this is my first post and I am not very experienced in PHP. Anyway, here goes:
I'm trying to write a script that writes out the contents of a folder along with some attributes of the files and folders within it. Here is the top half of the code that seems to be problematic:
For some reason, and under no particular circumstance that I can identify, files are sometimes identified as folders are are put into the array of folders ($dir_folders) and folders are identified as files. Can anyone offer a solution? Any help would be greatly appreciated since at this point I am completely stuck. Thanks in advance.
I'm trying to write a script that writes out the contents of a folder along with some attributes of the files and folders within it. Here is the top half of the code that seems to be problematic:
Code: Select all
$microtime_0 = microtime();
$dir_to_read = "./";
$dir_to_hide = array(".", "..");
$ext_to_hide = array("htaccess");
$ico_dir = "./ico/";
$date_format = "d-m-y g:i:s a";
//Get directory to read if provided in URL and assign a handler to the directory
if (isset($_REQUEST['dir'])) {$dir_to_read = $_REQUEST['dir'];}
$dir_handler = opendir($dir_to_read);
//Check that the directory to be read has a / at the end, if not, put one there
if (substr($dir_to_read, -1) != "/") {$dir_to_read .= "/";}
//Read the contents of each directory and put files in one array and folders in another
while (false !== ($file_readout = readdir($dir_handler))) {
if (is_dir($file_readout)) {
$dir_folders[] = $file_readout;
} else {
$delimiter_pos = strpos($file_readout, ".");
if (false == in_array(ltrim(substr($file_readout, $delimiter_pos), "."), $ext_to_hide)) {
$dir_files[] = $file_readout;
}
}
}
// Write out a list of the files and folders (debugging purposes only)
echo("<b>Folders</b>");
foreach ($dir_folders as $key => $value) {
echo($value."<br />");
}
echo("<b>Files</b>");
foreach ($dir_files as $key => $value) {
echo($value."<br />");
}