Forum Moderators: coopster

Message Too Old, No Replies

how to check if folders or directories exist?

         

paper_bass

9:18 am on Aug 21, 2003 (gmt 0)

10+ Year Member



Hi
I have been having some trouble checking directories / folders.

Basically I am trying to replicate the ASP code :

# # #
function doesfolderexist(foldername)
Set dfs=Server.CreateObject("Scripting.FileSystemObject")
If dfs.FolderExists(foldername) = true Then
doesfolderexist = true
Else
doesfolderexist = false
End If
set dfs=nothing

end function

# # #

So far I have looked into is_dir, file_exists etc but have been having some trouble with the path specified :

// to check to see if directory exists
//$path = dirname($_SERVER['PHP_SELF']); - not working? why not?
$path = 'C:\Inetpub\wwwroot\ba\d\sitex'; //works
$folder = 'graphics';
$test = $path.'/'.$folder;
print '<br>Path to Directory = ' . $test;
if (is_dir ($test)) {
print "<br><br>++Dir exists" . '[' . $test .']';
}
else {
print "<br><br>--Dir doesn't exist" . '[' . $test .']';
}

The code above will only find the graphics folder if the path is hard coded from absolute. If I try to use something like PHP_SELF or ./ in a path it doesn't work.

So what am I doing wrong? Should I be looking for the absolute path and then going to find directories?

If you can help I would be grateful - cheers - Ed

paper_bass

9:30 am on Aug 21, 2003 (gmt 0)

10+ Year Member



Just tried :

----------
// to see if directory exists
$dir = 'graphics';
if (file_exists($dir) && is_dir($dir)) {
print $dir . ' is a directory!';
}
else {
print $dir . ' is not directory';
}

----------

And it works fine... strange. Now I can look through all the directories in the main directory and see if they exist or not. But what I don't understand is why this should work when the previous method I tried didn't?

Could do with some help understanding this ... cheers if you can explain (I've been reading the php.net manual and a Core php manual but still don't understand)

coopster

12:33 pm on Aug 21, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Ed, I would recommend you start by printing your variable value out whenever you are finding difficulties. I've found this to be the best way to learn how PHP is handling your code. As a matter of fact, I'll often stop my script by using the exit function rather than print. I've "uncommented" your original path and added one line. Try printing right here to see what you are getting:

// to check to see if directory exists
$path = dirname($_SERVER['PHP_SELF']); - not working? why not?
exit("Path is: $path");
// $path = 'C:\Inetpub\wwwroot\ba\d\sitex'; //works
$folder = 'graphics';
$test = $path.'/'.$folder;
print '<br>Path to Directory = ' . $test;
if (is_dir ($test)) {
print "<br><br>++Dir exists" . '[' . $test .']';
}
else {
print "<br><br>--Dir doesn't exist" . '[' . $test .']';
}