note 70005 deleted from function.is-dir by googleguy

From: Date: Tue, 22 Oct 2013 14:59:16 +0000
Subject: note 70005 deleted from function.is-dir by googleguy
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-197273@lists.php.net to get a copy of this message
Note Submitter: alan dot rezende at light dot com dot br ---- The function is_dir will always return false if the handle acquired with opendir is not from the current working directory (getcwd); exception applies to "." and "..". Thus, if you need a consistent dir listing from any directory other than the current one, you must change dir first. Example follows: <?php chdir( '..' ); $rep=opendir('.'); while (false != ($file = readdir($rep))){ if (is_dir($file)){ print("<a href='$file/' class='text1'>$file</a>"); } } ?> The above code will list all directories (with links) for the ".." directory, relative to the current working dir. THE WRONG WAY TO DO IT FOLLOWS: <?php $rep=opendir('..'); while (false != ($file = readdir($rep))){ //print "$file is here (may be a dir or not)<br>"; if (is_dir($file)){ print "<a href='$file/' class='text1'>$file</a><br>"; } } ?> This code will turn into an empty list. If you want to make sure, uncomment the commented line... Regards...

« previous php.notes (#197273) next »