God damn directory problems!!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

God damn directory problems!!

Post by Gen-ik »

I'm using the following script to view folders and files on my server.

It works because it's showing all the folders and files.. which is good.

The problem is that the is_file() and is_dir() calls aren't doing their job properly. Some times they work, some times they don't!

I've tried a range of CHMOD values on the files and folders but they seem to have no effect.

If you can help... HELP!

Code: Select all

<?php

if ($dir=opendir($path))
{
while (($file = readdir($dir)) !== false)
{
if(is_dir($file)==true) { echo($file." [folder]<br>\n"); }
elseif(is_file($file)==true) { echo($file." [file]<br>\n"); }
else { echo ($file." [don't have a clue!]<br>\n"); }
}  
closedir($dir);
}

?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

hmmm... may be a clearstatcache(); at the beginning of the script helps....
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

Nope.. clearstatcache(); doesn't seem to have any effect.

The script works fine showing the files and folders that it is in.. but when I try to go one step deeper into the directory it doesn't seem to like it.

For example, the script is in /test and it shows everything how I want it to, and tell me what's a folder and what's a file.

Now, if I try to read /test/extras the script will read and display everything ok but won't be able to tell what's a folder and what is a file!
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

readir($dir) will only return the name of the file, not the directory.
You have to prepend the directory name.
e.g. is_dir($dir.'/'.$file)
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

readir($dir) will only return the name of the file, not the directory.
You have to prepend the directory name.
e.g. is_dir($dir.'/'.$file)
Ah... got it working. Thank-you :D
Post Reply