How do you distinguish a File and Directory?

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
lvlr
Forum Newbie
Posts: 3
Joined: Sun Sep 27, 2009 11:29 am

How do you distinguish a File and Directory?

Post 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?
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: How do you distinguish a File and Directory?

Post by Mark Baker »

are they symbolic links to your jpegs?
lvlr
Forum Newbie
Posts: 3
Joined: Sun Sep 27, 2009 11:29 am

Re: How do you distinguish a File and Directory?

Post 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?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: How do you distinguish a File and Directory?

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
zanus
Forum Newbie
Posts: 1
Joined: Mon Sep 28, 2009 3:22 am

Re: How do you distinguish a File and Directory?

Post 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";
 
lvlr
Forum Newbie
Posts: 3
Joined: Sun Sep 27, 2009 11:29 am

Re: How do you distinguish a File and Directory?

Post 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?
Post Reply