note 44697 added to function.is-dir

From: Date: Fri, 13 Aug 2004 08:03:19 +0000
Subject: note 44697 added to function.is-dir
Groups: php.notes 
Request: Send a blank email to php-notes+get-74578@lists.php.net to get a copy of this message
To make sure you get a true on is_dir you have to use the path. For example... I banged my head on a situation like this for a while: <?php if ($handle = opendir($_SERVER["DOCUMENT_ROOT"]."/img")) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { if (is_dir($file)){ echo "Dir: ".$file; } else { echo "File: ".$file; } echo "<br />"; } } closedir($handle); clearstatcache(); } ?> It always reads as a file even if one of the entries is a directory. The problem is that it has to see the whole path to recognize a directory. Thus the correct way is like this: <?php if ($handle = opendir($_SERVER["DOCUMENT_ROOT"]."/img")) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { + $path = $_SERVER["DOCUMENT_ROOT"]."/img" . "/" . $file; ! if (is_dir($path)){ echo "Dir: ".$file; } else { echo "File: ".$file; } echo "<br />"; } } closedir($handle); clearstatcache(); } ?> By the way, I found the solution on the PHP general mailing list. ---- Manual Page -- http://www.php.net/manual/en/function.is-dir.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+44697 Delete -- http://master.php.net/manage/user-notes.php?action=delete+44697&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+44697&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#74578) next »