is_dir() function problem

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
User avatar
ncic_1
Forum Newbie
Posts: 6
Joined: Wed Mar 03, 2004 9:35 am
Location: Crawfordville, Fl

is_dir() function problem

Post by ncic_1 »

Hey I am extremely new to PHP and I have never studied any other programming languages. So I feel like a huge dork. But I figure I will get better someday. Anyway here is my problem. I am trying to list all subfolders that exist under a certain folder in my site and filter out files. When I try to use is_dir($myDir) I cannot capture any directories. Maybe I need to fix something in my php configuration. I'm not sure. I'm not even sure if I am asking the correct question.

here is my code so far...

$rootDir = $_SERVER[DOCUMENT_ROOT];
$ncicDir = $rootDir . "/newcreationinchrist.com";

//open ncic directory and display it's contents
if($handle = opendir($ncicDir)){

echo "<br><br><b>Site Subdirectories: </b>";

while (false !== ($file = readdir($handle))){
if($file != "." && $file != ".."){

//capture only folders
if(is_dir($file)){
$folderName = $file . "Dir";
$folderName = str_replace(" ","_", $folderName);
$fixURL = str_replace(" ", "%20", $file);
$folderURL = $ncicDir ."/" .$fixURL;
echo "<br><br>$tab<a href=$folderURL><b>" . $folderName . "</b></a>";
}
}
}
closedir($handle);
}else{
echo "<br><br>--- error opening <b>$ncicDir</b> directory ---";
}


also I would like to extend this to loop through and list all subfolders of the subfolders of the ncicDir.

Thanks for any you help you guys might have. :D
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

IIRC it's because is_dir() needs the full path to the file/folder name, try:

Code: Select all

if (is_dir($rootdir.'/'.$file)) {
Mac
User avatar
ncic_1
Forum Newbie
Posts: 6
Joined: Wed Mar 03, 2004 9:35 am
Location: Crawfordville, Fl

Post by ncic_1 »

Thanks for your help. That works great. Now I have a new question that spawned from the last topic. I am able to collect the directories but not all the directories that exist under that ncicDir are being picked up. I was wondering if it had to change the mode of the folders that are not being recognized as directories. Thanks again.

Chris Anderson
User avatar
ncic_1
Forum Newbie
Posts: 6
Joined: Wed Mar 03, 2004 9:35 am
Location: Crawfordville, Fl

Post by ncic_1 »

oops Forget my last post i took a look at the code

Code: Select all

if(is_dir($rootdir.'/'.$file)){
and changed it to this

Code: Select all

if(is_dir($ncicDir . '/' . $file)){
and it works. Thanks again for all your help bro, it may seem like a small thing but I just couldn't figure it out and you clued me in to the problem right away.
Post Reply