Page 1 of 1

How do you distinguish a File and Directory?

Posted: Sun Sep 27, 2009 11:35 am
by lvlr
How do you distinguish a File and Directory?

I'm using:

Code: Select all

 
$dir = dir(".");
 
To get the contents of the current directory.

Then I need to know which items are directories and which are files, so I'm using:

Code: Select all

 
while (($file = $dir->read()) !== false){
 
if (is_file($file) == 1) {
 
But on some of my *.jpg images the is file returns are null value.

And the same using is_dir:

Code: Select all

 
while (($file = $dir->read()) !== false){
 
if (is_dir($file) == 1) {
 
Actually is_dir seems completely useless because the only items it ever identifies as directories are: the two directories in every directory: "." and "..".

Is this an error anybody else has ever had? Have they found a way around this?

Re: How do you distinguish a File and Directory?

Posted: Sun Sep 27, 2009 1:37 pm
by Mark Baker
are they symbolic links to your jpegs?

Re: How do you distinguish a File and Directory?

Posted: Sun Sep 27, 2009 1:45 pm
by lvlr
No they are not symbolic links, and I didn't actually mean it's only *.jpeg that behaves oddly. I should have said for example some *.jpeg's give null values. I have found *.gif's and other files with the same behavior.

I guess though the biggest thing that surprises me is: "is_dir" doesn't work, has anybody else had this problem? Is it possibly my server?

Re: How do you distinguish a File and Directory?

Posted: Sun Sep 27, 2009 10:57 pm
by s.dot
I have problems with is_file() and is_dir() on my localhost server (windows).

Try using absolute paths instead of relative paths (or the other way around).. I think that worked for me.

Re: How do you distinguish a File and Directory?

Posted: Mon Sep 28, 2009 3:46 am
by zanus
I would just use scandir() http://php.net/scandir on everything.

I mean..if it isn't a directory then it's obviously a file..so it should be safe to say you could do this

Code: Select all

 
if(scandir("somepath/to/a/file.txt"))
    echo "It's a directory";
else
    echo "It's a file";
 

Re: How do you distinguish a File and Directory?

Posted: Tue Sep 29, 2009 12:02 pm
by lvlr
Well that was closer, but still didn't do it. Now it errors on the side of saying some directories are files via returning null when I use Scandir. Therefore scandir(path/directory)=null, even when the directory has files in it!

W.T.F? is is_file supposed to be used for if not to determine whether or not the thing is a file? Same for is_dir?