Simple solution to resolve is_dir or is_link confusion

From: Date: Tue, 04 Aug 2009 21:24:34 +0000
Subject: Simple solution to resolve is_dir or is_link confusion
Groups: php.webmaster 
Request: Send a blank email to php-webmaster+get-5602@lists.php.net to get a copy of this message
Hello Webmaster, Can I see there is some confusion on how to decide if a readdir result is a directory, file or symbolic link. After looking over the comments I decided to try something on my own. The solution I came up with seems logical to me. Maybe someone in the PHP world find it usefull. extract($_SERVER); if ($handle = opendir($DOCUMENT_ROOT)) { //just to grab a starting point echo "Directory handle: $handle\n"; echo "Files:\n"; /* This is the correct way to loop over the directory. */ while (false !== ($file = readdir($handle))) {
       if ($file != "." && $file != "..") { // filter out the dots of dirs
           $islink = false; // initialize logical islink to false
           if(is_link("$DOCUMENT_ROOT/$file")){  // deciding if it is a link first knocks out all other possibilities
               echo "<li>Link: $file</li>\n";                $islink = true;
           }
           if(is_file("$DOCUMENT_ROOT/$file") && !$islink){
               echo "<li style='color: blue'>File: $file</li>\n";
           }                      if(is_dir("$DOCUMENT_ROOT/$file") && !$islink){
               echo "<li style='color: red'>Folder: $file</li>\n";
           }
       }
} closedir($handle); } I would like some feedback if possible. Kind Regards Joe Rasberry -- W3webmaster.com, LLC Joe Rasberry, President 16 Greenbriar Rd. Oxford, CT 06478 U.S.A. (203) 881-3100 - (203) 260-6067 (cell) LEGAL NOTICE: Unless expressly stated otherwise, this message is confidential and may be privileged. It is intended for the addressee(s) only. If you are not an addressee, any disclosure, copying or use of the information in this e-mail is unauthorized and may be unlawful. If you are not an addressee, please inform the sender immediately and permanently delete and/or destroy the original and any copies/printouts of this message. Thank you.

« previous php.webmaster (#5602) next »