...

View Full Version : Resolved PHP Menu Generator Not Ignoring Subdirectories



Rowsdower!
05-06-2009, 05:04 PM
Nevermind, I figured it out!

-------------------------------------------------------------------
I have built my first PHP menu generator. This is the script I'm using to generate a menu system:

<?php
$path = $_SERVER['DOCUMENT_ROOT']."/php/";
$dir_handle = @opendir($path) or die("Unable to open $path");
while ($file = readdir($dir_handle))
{
if($file!="." && $file!=".." && $file!="index.php" && !strpos($file, ".html") && !is_dir($file)){
$file_copy = $file;
$file_page = $file;
$file_page = str_replace(".php","",$file_page);
$file_page = str_replace(".html","",$file_page);
$file_page = str_replace(".css","",$file_page);
$file_copy = str_replace("_"," ",$file_copy);
$file_copy = str_replace(".php","",$file_copy);
$file_copy = str_replace(".html","",$file_copy);
$file_copy = str_replace(".css","",$file_copy);
$file_copy = str_replace("php","PHP",$file_copy);
$file_copy = str_replace("html","HTML",$file_copy);
$file_copy = str_replace("css","CSS",$file_copy);
$file_copy = ucwords($file_copy);
echo " <li><a href=\"/php/?page=$file_page\"";

if(strlen($file_copy)>10 && strlen($file_copy)<=18){
echo " style=\"width:11.5em;\"";
}
else if(strlen($file_copy)>18 && strlen($file_copy)<=26){
echo " style=\"width:13em;\"";
}
else if(strlen($file_copy)>26 && strlen($file_copy)<=40){
echo " style=\"width:20em;\"";
}
echo ">$file_copy</a></li>\n";
}
}
closedir($dir_handle);
?>

I know the code isn't optimized and I'm using some extra instances of variables when I don't need to, but for now I'm more concerned with the following:

This code is in a file called "menu.php" which is located in the root directory. As you can see, I am filtering out ".", "..", the index page, and any ".html" files located in the directories. I'm also using !is_dir($file) in the if statement to prevent subdirectories from being returned as menu <li> items.

The code above works perfectly when the menu.php file is included in the "php" directory, however when the same include is made to a file in any other directory the menu lists the subdirectories under the php folder. The ".", "..", the index page, and ".html" files are still properly ignored but for some reason the subdirectories pop back in.

I think this is the only really relevant code:
<?php
$path = $_SERVER['DOCUMENT_ROOT']."/php/";
$dir_handle = @opendir($path) or die("Unable to open $path");
while ($file = readdir($dir_handle))
{
if($file!="." && $file!=".." && $file!="index.php" && !strpos($file, ".html") && !is_dir($file)){

...

}

And I can't find anything in there to make sense of this problem.

As I understand it this should just be loading the same exact menu only in a different page but somehow it seems to be tied to the directory that houses the page that is calling up the menu (if that makes sense). Can anyone tell me why this is behaving like this and how I can stop it?

This is the page I am using it on (http://rowsdower.freehostia.com/). Let me know if I need to provide more info.
-------------------------------------------------------------------



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum