Page 1 of 1

is_dir() function problem

Posted: Wed May 21, 2003 6:16 pm
by jakobdoppler
when i use this script ,he prints every directory in bold letters with DIRECTORY written in front



$handle=opendir ('.');
echo "Verzeichnis-Handle: $handle\n";
echo "<br>Dateien:\n <br>";
while (false !== ($file = readdir ($handle))) {
if(!is_dir($file))
echo "$file\n <br>";
else
echo "<b>DIRECTORY $file\n</b><br>";


but if i choose another directory to display, like opendir('.\modules');
a subdirectory which contains folders, he doesn't display them in bold letters but in normal plain text like usually only files are displayed
why this ?

Posted: Wed May 21, 2003 7:34 pm
by volka
readdir() does not return the directory name, only the filename.
e.g. you've opened the directory test and there was file/directory subdir in. You had to check test/subdir but your script only checks subdir <=> ./subdir. Prepend the directory name.

requestion

Posted: Thu May 22, 2003 4:15 am
by jakobdoppler
when i use readdir
i get every file/directory name in this directory
so usually all directories & all files in this folder are displayed

when i use opendir(.) he DOES display me all directories in bold letters
and files are not, so when i choose another path
like
.\modules he does not , although there are also files and directories that he returns

but then is_dir doesn't work

i think i think completely wrong ,am i ?
please can u exlain it why i need to prepend the dir , and why this is not necessary with (.)

or even better would be the modified code to do what i want
(just marking directories bols and leaving files blank)

Posted: Fri May 23, 2003 2:00 am
by volka
let's say you have a directory structure like
  • /
    • www/
      • htdocs/
        • script.php
          another.file
          • data/
            • a.dat
              b.dat
script.php is beeing executed. opendir('.') openes the current workingdirectory which is /www/htdocs/ in this case. If you do not prepend any directory name ./ will be used automatically, so is_dir('another.file') is the same as is_dir('./another.file').
But if you open the directory data (same as ./data) readdir will return (e.g.) a.dat. If you put only that name to is_dir it tries to get the info for is_dir('./a.dat'); which doesn't exist.
is_dir('data/a.dat'); would be correct.

Code: Select all

<?php
$path = 'data';

$dd = opendir($path);
$path .= '/';
while( ($filename = readdir($dd)) !== FALSE)
	echo $filename, (is_dir($path.filename)) ? ' is a directory' : ' is not a directory';
?>

thanx

Posted: Fri May 23, 2003 4:23 pm
by jakobdoppler
:D


ahh know i got it, thank u very much !!