Vici0usX Posted June 13, 2011 Share Posted June 13, 2011 With my directory/index listing script and I'm trying to figure out how I can put a list of folders from the parent directory on every page or sub directory. I figured out how to display folders if in the parent directory already, but I want to show the parent folders in every single page. here is an example: 1. I go into Folder "Manuals", the subfolders are A, B, C, D, E, F, G.. so fourth 2. When I go into sub folder "B," I'd like to see at the top of every page "A,B,C,D,E,F" from Manuals (Parent Folder) so my users can click on any sub directory of Manuals without pressing the back button. Here is my script.. <?php # Name of this script (to be left out of directory listing) $THIS_SCRIPT = getenv("SCRIPT_NAME"); # Read path argument, if blank set to "." $dir=$_GET['dir']; if ($dir=="" || $dir==false) { $dir="."; } # Determine absolute path (include trailing / if not blank) $absdir = realpath($dir); if ($absdir != "") { $absdir .= "/"; } # Directory where script lives (include trailing / if not blank) $scriptdir = getcwd(); if ($scriptdir != "") { $scriptdir .= "/"; } # Insure that absolute dir is under the current directory # This prevents users from submitting a dir argument # that reaches outside the directory where this script resides. $pos = strpos($absdir,$scriptdir); if ($pos !== 0) { echo "<b>ERROR</b>: An invalid directory (<b>$dir</b>) was entered."; exit(); } # Get clean reldir (need for file and directory URLs) $reldir = substr($absdir,strlen($scriptdir)); # Refresh PHP's (OS's ?) file and directory list cache clearstatcache(); # Read directories and files in current directory $handle = opendir($absdir); while (false !== ($filename = readdir($handle))) { # Add directory to list if (is_dir($absdir."/".$filename)==true && $filename!=".") { $dirs[] = $filename; } # Add file to list (omit this script) if (is_dir($absdir."/".$filename)==false && $filename!=$THIS_SCRIPT) { if ($SHOW_DOT || substr($filename,0,1)!=".") { $files[] = $filename; } } } # Get parent directory unless current directory # is the same as the directory of this script. $at_topdir = $absdir==$scriptdir; if (! $at_topdir) { $absparentdir=""; $subdirs=explode("/",$absdir); for($x=1;$x<=count($subdirs)-3;$x++) { $absparentdir.="/".$subdirs[$x]; } } # Get relative parent directory $relparentdir = substr($absparentdir,strlen($scriptdir)); # Sort file and directory list if ($files) { sort($files); } if ($dirs) { sort($dirs); } # Show current directory if ($reldir=="") { $showdir = "."; } else { $showdir = $reldir; } echo " <div id=\"container\"><div id=\"row\"> <div id=\"left\"> <a href=\"home.php\"><h3 class=\"logo\">Test Test</h3></a></div> <div id=\"right\"><a href=\"home.php\"><h3 class=\"logo\">→ Home</h3></div> </div></div> <div class=\"content\"> <h1 class=\"page_header\"><a href=\"test.php\">Test</a> » $showdir</h1>"; $url = htmlspecialchars($_SERVER['HTTP_REFERER']); if ($dirs) { foreach($dirs as $name) { # List child directory if ($name!="..") { echo "<div class=\"folder\"><a href=\"$THIS_SCRIPT?dir=$reldir$name\">$name</a></div>"; } } } else { print " \n"; } echo "<div class=\"filesBG\">"; # List files if present echo "\n"; if ($files) { foreach($files as $name) { # Form relative path to file by removing leading /. echo "<h2 class=\"files\"><img src=\"/images/file.gif\" alt=\"Download\"></img><a href=\"$reldir$name\" _target=\"_self\">$name</a></h2>\n"; } } else { print " \n"; } echo "</div>\n"; # Finish table echo " </div>\n"; ?> I appreciate the help, thank you! Quote Link to comment Share on other sites More sharing options...
Psycho Posted June 13, 2011 Share Posted June 13, 2011 Use dirname() on the current directory (which I assume to be $absdir) to return the parent directory. Then get the sub-directories from that. Also, instead of using readdir() to read through the objects in a directory you might want to look into using glob(). It will return an array of the objects and you can have it filter out files and/or folders based upon your needs. So you wouldn't need any is_dir() to test if something is a file or a folder. Also, you don't have to worry about '.' or '..' Quote Link to comment Share on other sites More sharing options...
Vici0usX Posted June 13, 2011 Author Share Posted June 13, 2011 Oh I see, I could try doing that. Thank you very much Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.