Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > [PHP] Files/directories not being recognised as such

[PHP] Files/directories not being recognised as such
Thread Tools
Oisín
Moderator Emeritus
Join Date: Mar 2004
Location: Copenhagen
Status: Offline
Reply With Quote
Sep 6, 2007, 01:48 PM
 
I’m trying to make a small function that lists (certain) directories and files in a traditional filetree format. Obviously, I’ll need, then, to separate files and directories, since a directory should be a link to expandable lists of files and directories within that directory, while files should just be links to the file itself.

I’m doing this in PHP, and you wouldn’t think I’d have this much trouble with it, but I am. See, PHP doesn’t seem to want to recognise my directories as directories.

Basically, the code I have is this, for simple testing purposes:

Code:
$rootdir = '/path/to/root/dir'; $df = scandir($rootdir); echo '<p>'; foreach ($df as $sgdf => $sgdfval) { if ($sgdfval != 'array-of-dirs/files-not-to-be-included') { echo $sgdfval; if (is_dir($sgdfval)) { echo ' (dir)<br>'; } else if (is_file($sgdfval)) { echo ' (file)<br>'; } else { echo ' (huh?)<br>'; } // end if } // end if } //end foreach echo '</p>';
– so, basically, I want it (for now) to just add a “(dir)” after the directory/file name if it’s a directory, or a “(file)” if it’s a file—or give it a “(huh?)” if it’s neither (which would be..?).

However, that’s not quite what’s happening. Rather, it’s only recognising ‘.’ and ‘..’ as directories; all other files and directories are apparently neither directories nor files in the eyes of PHP, so everything else (files and directories alike) takes the “(huh?)” option.

I’ve been reading through the various directory functions in the documentation on php.net, and I can’t find any reason why this is. Shouldn’t a directory be a directory, regardless? And a file a file? What can be wrong?

[This is a school server, so I don’t really know anything about the setup, apart from the fact that it’s an Apache server]
     
besson3c
Clinically Insane
Join Date: Mar 2001
Location: yes
Status: Offline
Reply With Quote
Sep 6, 2007, 01:55 PM
 
is $sgdfval the full absolute or relative path to the directory? Is it of type string?
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Sep 6, 2007, 02:10 PM
 
I think you want $rootdir.$sgdfval — scandir() only returns the names.

By the way, you might want to look into more meaningful variable names unless these are in another language.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Oisín  (op)
Moderator Emeritus
Join Date: Mar 2004
Location: Copenhagen
Status: Offline
Reply With Quote
Sep 6, 2007, 02:18 PM
 
Originally Posted by besson3c View Post
is $sgdfval the full absolute or relative path to the directory? Is it of type string?
Not sure what you mean here... $sgdfval is just the name of the file itself, stripped of path, as far as I know. $rootdir is the relative path, I think, though I’m not good on these nomenclatures—it’s expressed as /net/home.server.com/xport/home1/rootdirectory (actual values changed).

I tried using $rootdir . $sgdfval instead (in the is_dir() parts), but that made no difference.

I figure it must be parsing the code and locating the directories/files properly, since a) all files and directories that are supposed to appear do appear, and b) it recognises ‘.’ and ‘..’ as directories—though perhaps this is hard-coded into PHP, so that it will always recognise those two entities as directories, regardless of context?

Edit: Sorry, my bad! I’d somehow managed to switch things around, so I’d defined the variable as $dirroot to begin with. Obviously, using $rootdir . $sgdfval wouldn’t work, then! Fixed that, and now it’s working—thanks a mill, Chuckit

Probably a n00b error/question, but why does PHP require the full path in order to tell whether it’s a file or a directory?
     
besson3c
Clinically Insane
Join Date: Mar 2001
Location: yes
Status: Offline
Reply With Quote
Sep 6, 2007, 02:22 PM
 
So $sgdf is some sort of array key, and $sgdfval is the actual filename? Is there a trailing slash on your directories? If so, try taking that off...
     
besson3c
Clinically Insane
Join Date: Mar 2001
Location: yes
Status: Offline
Reply With Quote
Sep 6, 2007, 02:23 PM
 
Why don't you show us some sample output from your script so that we can see what sorts of values $sgdfval is being assigned?
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Sep 6, 2007, 02:45 PM
 
Originally Posted by Oisín View Post
Probably a n00b error/question, but why does PHP require the full path in order to tell whether it’s a file or a directory?
Well, it can't just guess from the name whether it's a file or directory; it needs to look at the actual file. And to do that, it needs to know where the file is. If you specify a relative path, it will assume you're talking about the current working path (I think it's DOCUMENT_ROOT by default).

Incidentally, I believe the reason it would return information only for . and .. in this case is that . and .. are the only entries in common between the current working directory and the directory you got the list of names from.

Originally Posted by besson3c View Post
So $sgdf is some sort of array key, and $sgdfval is the actual filename?
The scandir() function returns an array of filenames in the directory named, so $sgdf would be the index of the filename in the array and $sgdfval would be the filename itself.
( Last edited by Chuckit; Sep 6, 2007 at 02:53 PM. )
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
Oisín  (op)
Moderator Emeritus
Join Date: Mar 2004
Location: Copenhagen
Status: Offline
Reply With Quote
Sep 6, 2007, 02:51 PM
 
So $sgdf is some sort of array key, and $sgdfval is the actual filename? Is there a trailing slash on your directories? If so, try taking that off...
Yup, $sgdf is the key, $sgdfval is the value. I just realised, though, that specifying the key is completely unnecessary, so it’s just foreach ($dirfil as $sgdfval) now.

Well, it can't just guess from the name whether it's a file or directory; it needs to look at the actual file, and to do that, it needs to know where the file is. If you specify a relative path, it will assume you're talking about the current working path.
Ach, I’m still thinking too much like a human being and not enough like a computer—it just used the full path in order to output the list of directories, but that doesn’t, of course, mean that that path is still automagically available here, when I’m using the variable that stores only the file/directory name in a nested different function.

Incidentally, I believe the reason it would find only . and .. in this case is that . and .. are the only entries in common between the current working directory and the directory you got the list of names from.
That makes perfect sense. Things have a tendency to start making sense once the smart people in here explain them to you.
     
   
Thread Tools
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 05:43 PM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,