Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with opendir($dir) function

Status
Not open for further replies.

chrvi

Programmer
Mar 9, 2005
3
CZ
Hello,
I've got a problem with opendir($dir) function. When I use opendir(".") everything is OK but when I call opendir($dir), program works strange - is_dir() and is_file() return wrong results then.
For example (both scripts are written bellow):
I have a directory "test1" with "Script1.php" and directory "test1/test2" with "Script2.php".
Both scripts are equal except for opendir parameter.
Script2.php (using opendir(".")) works just fine.
Script1.php (using opendir("test2") and writing out the content of the same (test2) directory as Script1.php) works incorrectly - it can't say, if $file is a directory or a file (is_dir and is_file both returns false).

Please, help me to fix it. How to tell the exact path to an opendir() function since "dir", "dir/", 'dir', 'dir/' doesn't work and the only parameters that works fine for me are "." or ".." (and '.', './' and so on...).
I'm feeling quite helpless.

I'd be grateful for any advice.

Radek


======Script1.php in test1 directory=================

$handle=opendir("test2");
//not ".", it's the only difference between the two scripts

while (false!==($file = readdir($handle)))
{
if ($file != "." && $file != "..")
{
if (is_dir($file))
echo "Dir $file <br>\n";
elseif (is_file($file))
echo "File $file <br>\n";
}
}
closedir($handle);

====Script2.php in test2 directory=================

$handle=opendir(".");

while (false!==($file = readdir($handle)))
{
if ($file != "." && $file != "..")
{
if (is_dir($file))
echo "Dir $file <br>\n";
elseif (is_file($file))
echo "File $file <br>\n";
}
}
closedir($handle);

==================================================
 
The problem is not with opendir(). The problem is your code. When you are opening a subdirectory, you are probably getting filenames back, but you don't know it, as your script will only show the files if one of your tests works.

However, since the files you're testing are in the subdirectory, the invocation of is_file() and is_dir() must include that subdirectory.

Try this version of your script:

Code:
<?php
$directory = 'test2';
$handle=opendir($directory);

while ($file = readdir($handle)) 
{
	if ($file != "." && $file != "..") 
	{ 
		print $file . ":";
		if (is_dir($directory . '/' . $file))
		{
			echo "dir";
		}
		else
		{
			if (is_file($directory . '/' . $file))
			{
				echo "file";
			}
			else
			{
				echo 'error';
			}
		}
	}
	
	print "\n";  //from a web interface, change this to '<br>';
}
closedir($handle);


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Have you considered to use the full file system path?
Inspect the current working directory in the two scripts - you'll see what the real file system path is:
Code:
echo getcwd() . "\n";
Your relative path might work from the current location, but a full absolute path is failsafe.
 
How about passing "./test2" in Script1 above. Or better yet... pass in the complete logical path to the directory? Just a thought... since you may have missed those in your testing.

Jeff
 
Thanx for your advices very much.
I still don't understand it but it works.
Do you always use the full path while working with subdirs or do you always call a is_dir($dir.'/'.$file)?

Here's a new code for Script1.php with some
"question-comments".

$dir = 'test2';
$handle=opendir($dir);
if ($handle) echo "Dir $dir opened"; //OK
else
{ /* I don't get here, so $dir has been accepted and opened as a directory, hasn't it?
Nevertheless, getcwd() still returns "test1", not "test2".
And if $handle doesn't contain a correct directory string,
what does $handle means?
*/
}

while (false!==($file = readdir($handle)))
{
echo "Current dir ".getcwd()."<br>\n"; //returns .../test1
echo "but \$dir is $dir<br>\n"; //returns "test2"

if ($file != "." && $file != "..")
{
if (is_dir($dir.'/'.$file)) //now it works
echo "Dir $file <br>\n";
elseif (is_file($dir.'/'.$file)) //now it works
echo "File $file <br>\n";
else
echo "Unrecognised item $file<br>\n";
// This has always happened
// with is_dir($file) or is_file($file)
// All items from $dir were written out
// (so they were accessed) but were not
// recognised either as dirs or as files.
// How is it possible? What were these items
// considered to be?
}
}
closedir($handle);
 
Jeff: "test2" and "./test2" worked equally for me.
 
After opendir(), $handle contains, well, a handle. It's similar to a file handle. It does not contain the data, bur rather a means of accessing the data.

I always use a relative path, yes. Think about what your script is doing. It's in the directory test1 and opens a handle to test2. Any invocations of is_file() or is_dir() is not relative to the handle, but relative to where the script is running. So if you want to find about a file that's in test2, you have to include a relative path to the file.

The reason your original script was never recognising any filename as a file or directory was because the script wasn't finding the file. It was looking in the current directory, test1, but the filenames were from a listing of test2.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top