|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #22499 is_dir doesn't always work
Submitted: 2003-03-01 18:00 UTC Modified: 2003-05-09 07:30 UTC
Avg. Score:4.7 ± 0.5
Reproduced:12 of 12 (100.0%)
Same Version:5 (41.7%)
Same OS:5 (41.7%)
From: itamarc at rogers dot com Assigned:
Status: No Feedback Package: Directory function related
PHP Version: 4.3.1 OS: windows xp
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: itamarc at rogers dot com
New email:
PHP Version: OS:


 [2003-03-01 18:00 UTC] itamarc at rogers dot com
is_dir doesn't work unless I use it as !is_dir and even then a few directories are still read as files.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2003-03-02 05:00 UTC]
Not enough information was provided for us to be able
to handle this bug. Please re-read the instructions at

If you can provide more information, feel free to add it
to this bug and change the status back to "Open".

Thank you for your interest in PHP.

Let's see an example script, along with a directory listing. Keep it short please.
 [2003-03-02 11:50 UTC] itamarc at rogers dot com
the is_dir is supposed to come back true if reading a directory and false if not.  it doesn't.  it always comes back false.
if used as (not)!is_dir then it works right most of the time but still give the wrong answer for some directories.
 [2003-03-02 12:25 UTC]
Yes, you said that already.

Let's see the script you used which exposes this bug.
For example - this works for me, using 4.3.1 on Windows 2k with apache:
header('Content-Type: text/plain');
$dir = opendir('.');
while($entry = readdir($dir))
                echo "$entry is a dir\n";
                echo "$entry is NOT a dir\n";
 [2003-03-02 16:21 UTC] itamarc at rogers dot com
let's see.  using apache2 on win xp pro


if (!empty($_GET['directory'])) {
	$temp = $_GET['directory'];
	$d = dir("d:/inetpub/wwwroot/$temp");
	$old = $_GET['old'];
} else {
	$d = dir("d:/inetpub/wwwroot");
	$old = "";

echo "<h3>Path: ".$d->path."</h3><br>\n";

$dir = $d->path;
$totaldirsize = 0;
$mark = 1;

print "<table width=\"100%\"><tr><td width=\"10%\"><u>File Name:</u></td><td><u>File Size:</u></td></tr>\n";
print "<tr><td colspan=2><hr></td></tr>\n";

while (false !== ($entry = $d->read())) {
	if (!is_dir($entry)) {
		$file = filesize($entry);
		$totaldirsize = $totaldirsize + $file;
		if ($file > 1023) {
			$file = $file / 1024;
			if ($file > 1023) {
				$file = $file / 1024;
				if ($file > 1023) {
					$file = $file / 1024;
		$file = round($file, 3);
		print "<tr><td><a href=\"$dir/$entry\">$entry</a></td>\n";
		if ($mark == 1)
			print "<td>$file Bytes</td></tr>\n";
		elseif ($mark == 2)
			print "<td>$file KBytes</td></tr>\n";
		elseif ($mark == 3)
			print "<td>$file MBytes</td></tr>\n";
		elseif ($mark == 4)
			print "<td>$file GBytes</td></tr>\n";
			print "<td>\"$file\" \"$mark\" UNKNOWN FILE SIZE</td></tr>\n";
		$mark = 1;
	} else {
		if ($entry == ".") {
			print "<tr><td><a href=\"siteadmin.php?directory=\">$entry</a></td><td>directory</td></tr>\n";
		} elseif ($entry == "..") {
			print "<tr><td><a href=\"siteadmin.php?directory=$old\">$entry</a></td><td>directory</td></tr>\n";
		} else {
			$direc = "$dir/$entry";
			for ($i = 19; $i <= (strlen($direc) - 1); $i++) {
				$direct = $direc{$i};
				$dire = "$dire$direct";
			for ($i = 19; $i <= (strlen($dir) - 1); $i++) {
				$temp = $dir{$i};
				$dirtemp = "$dirtemp$temp";
			$old = $dirtemp;
			print "<tr><td><a href=\"siteadmin.php?directory=$dire&old=$old\">$entry</a></td><td>directory</td></tr>\n";
			$dire = "";

print "</table><hr><br>\n";

if ($totaldirsize > 1023) {
	$totaldirsize = $totaldirsize / 1024;
	if ($totaldirsize > 1023) {
		$totaldirsize = $totaldirsize / 1024;
		if ($totaldirsize > 1023) {
			$totaldirsize = $totaldirsize / 1024;
$totaldirsize = round($totaldirsize, 3);

if ($mark == 1)
print "<td>$totaldirsize Bytes</td></tr>\n";
elseif ($mark == 2)
	print "$totaldirsize KBytes Total\n";
elseif ($mark == 3)
	print "$totaldirsize MBytes Total\n";
elseif ($mark == 4)
	print "$totaldirsize GBytes Total\n";
	print "\"$totaldirsize\" \"$mark\" UNKNOWN TOTAL FILE SIZE\n";


 [2003-03-02 19:05 UTC]
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit as this bug system is not the
appropriate forum for asking support questions. 

Thank you for your interest in PHP.

Next time: make the script shorter, please! We don't need the size stuff.

IIC - when the first entry is a directory it will always fail, because you only change dirs after you read a file.

Make the test:

and you'll see it'll always work.
 [2003-04-17 16:47 UTC] mark_ford_uk at yahoo dot co dot uk
(currently using php4.2.2)

I too have discovered directories are not always recognised,.  However, I have found they are correctly recognised provided I ensure the path has a '/' on the end.

This bug appears to be widely reported and to my knowledge has not been fixed even in recent versions.  Can someone please investigate?

 [2003-05-09 07:30 UTC]
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Open". Thank you.

PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Feb 22 19:01:29 2024 UTC