is_dir() function problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jakobdoppler
Forum Commoner
Posts: 46
Joined: Wed May 21, 2003 6:16 pm

is_dir() function problem

Post 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 ?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
jakobdoppler
Forum Commoner
Posts: 46
Joined: Wed May 21, 2003 6:16 pm

requestion

Post 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)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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';
?>
jakobdoppler
Forum Commoner
Posts: 46
Joined: Wed May 21, 2003 6:16 pm

thanx

Post by jakobdoppler »

:D


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