directory listing

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
sheepz
Forum Commoner
Posts: 58
Joined: Wed Jul 06, 2005 11:35 pm

directory listing

Post by sheepz »

feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I want to make a list just like "directory listings" on an IIS server.  It shows the list of programs on a folder.  I want to be able to secure that list.  I dont want directory listings to be enabled for security reasons.  Is there a way I can modify this simple script so it would perform like a directory listing?

Code: Select all

<?  
$dir_name="/Inetpub/wwwroot/";  
$dir=opendir($dir_name);  
$file_list="<ul>";  

  while ($file_name=readdir($dir))  
    {  
       if (($file_name!=".") && ($file_name!=".."))  
            {  
     $file_list .="<li> <a href=\"".$dir_name.$file_name."\">$file_name</a>";  
            }  
     }  
$file_list .="</ul>";  
closedir($dir);  
?>
This makes a list and makes the links but when I click on the links it fails, says page cannot be displayed. Thanks for help.


feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
sheepz
Forum Commoner
Posts: 58
Joined: Wed Jul 06, 2005 11:35 pm

Post by sheepz »

sorry about that I thought i put the ["code"] and ["/code"] on my php code. Here is what is automatically generated by IIS with "directory browsing" checked. this is done thru "view source"

Code: Select all

<head><title>localhost - /scripts/</title></head><body><H1>localhost - /scripts/</H1><hr>

<pre><A HREF="/">[To Parent Directory]</A><br><br>      Tuesday, March 28, 2006  5:47 PM        <dir> 
<A HREF="/scripts/APPLICATIONS/">APPLICATIONS</A><br>     Thursday, March 23, 2006  4:49 PM         2088 
<A HREF="/scripts/BACKUP_UTL.bat">BACKUP_UTL.bat</A><br>      Tuesday, March 21, 2006  8:46 AM         2418 
<A HREF="/scripts/FirewallPortAdding.bat">FirewallPortAdding.bat</A><br></pre><hr></body>
the result is

Code: Select all

localhost - /scripts/

--------------------------------------------------------------------------------

[To Parent Directory]      Tuesday, March 28, 2006  5:47 PM        <dir> APPLICATIONS     
Thursday, March 23, 2006  4:49 PM         2088 BACKUP_UTL.bat      
Tuesday, March 21, 2006  8:46 AM         2418 FirewallPortAdding.bat
I like how directory browsing works but it has no security. I want all the batch files to be accessible by making a list of them and automatically making a hyperlink to the file. If I use directory browsing anyone can just type in \\servername\scripts and be able to access it. I want to be able to do the same technique with a loop so I can add on top of the code an IF statement to check username and password before allowing access.
sheepz
Forum Commoner
Posts: 58
Joined: Wed Jul 06, 2005 11:35 pm

Post by sheepz »

i want to have a script that will work just like "directory browsing" i found this script on this site, i'm not sure where, but it does work. the problem that i do have is when i change the $path = "/scripts/"; the folders with in that folder wont open. it gives me a "page cannot be found" but if i use $path = "./"; it works fine, the folders within that folder would open just like "directory browsing" the reason i need this to be /scripts/ is because i dont want the users to be able to access the wwwroot folder and it's contents. is there something wrong with the script that when i change the $path it wont open subfolders? if i use the "./" root folder it opens subfolders fine.

Code: Select all

<html>
<head>
<title> Directory List</title>
</head>
<body>
<h2> Directory listing </h2>


<?
$path = "./";
$dir = opendir($path);
echo "<ul>\n";

while($file = readdir($dir))
	{
	if ($file != "." && $file != ".." && $file != ".php")
		{
		echo "<li><a href = \"" .$file. "\" target = \" _blank\"> ".$file." </a></li> \n";
		}
	}
echo "</ul>\n";
closedir($dir);
?>
</body>
</html>
User avatar
ed209
Forum Contributor
Posts: 153
Joined: Thu May 12, 2005 5:06 am
Location: UK

Post by ed209 »

I made something like this a while ago... it's a bit of a mess, so if you improve on it - post it back here. It's like a directory browser, I have only included directory / file names and no other info.

All you need to do is drop it into the directory you want to be able to drill down from. You can also add an array of files / folders that you don't want displayed.

Code: Select all

<?php
// the name of this PHP script
$this_filename = "directory_browser.php";

// get variables sent to page
	$directoryName=$_REQUEST['directoryName'];
	
	
	
	if(!isset($directoryName)){
		$pwd = dirname(__FILE__);
		$directory_array = explode("/", $pwd);
		$directoryName = "../".$directory_array[count($directory_array) - 1];
	}
	
	// checks the files in the directory passed then returns them in $folderContent
	function listDirContents($directoryName){
		
		// Files / directories that you don't want to show
		$dontShow = array($this_filename, '.',  '..', '.DS_Store', '.htaccess', '.htpasswd');
	
		$cr = "\n\r"; // line breaks to make html easier to read
	
		if(!($dp = opendir($directoryName))) die("Sorry, your request can not be completed. Please try again");
			while($file = readdir($dp))
				if(!in_array($file, $dontShow)){				
					$filenames[] = $file;
				}
				closedir($dp);
				
		$folderContent ='';
		
			if(count($filenames) < 1){
				$folderContent .= '<br /><br />This folder is empty';
			}
		
		for($dup=0; $dup < count($filenames); $dup++){
		
			if(count($filenames) < 1){
				$folderContent .= '';
			}else{
			
				if(is_file($directoryName."/".$filenames[$dup])){
					$folderContent .= '<li><a href="'.$directoryName.'/'.$filenames[$dup].'"  target="_blank" class="a_file">'.$filenames[$dup].'</a></li>'.$cr;
				}else{
					$folderContent .= '<li><a href="'.$this_filename.'?directoryName='.$directoryName.'/'.$filenames[$dup].'" class="a_dir">'.$filenames[$dup].'</a></li>'.$cr;
				}
			}
		}
		
		return $folderContent;
	}
	
	
	
	
	function breadCrumb($directoryName){
		
		$bread = "You are here: ";
		$linkDirectory = "..";
		
		$locations = explode("/", $directoryName);
		
		for($i=0; $i < count($locations); $i++){
			if($i > 0){
				$linkDirectory .= "/".$locations[$i];
				
				$bread .= "<a href=\"$this_filename?directoryName=".$linkDirectory."\"><span class=\"location\">".$locations[$i]." </span></a> / ";
			}
		}
		
		$bread .= "";
		
		return $bread;
	}
	

	
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Hereward Asstes</title>

<style type="text/css" media="screen">
<!--
	body{
		margin:0;
		padding:0;
		font-family:Verdana, Arial, Helvetica, sans-serif;
	}
	body img{
		border-bottom:2px solid #999999;
	}
	body hr{
		margin:0;
		padding:0;	
		width:700px;
	}	
	body h1{
		margin:10px 10px 0px 10px;
		padding:0;	
		font-size:18px;
		color:#333333;
		font-weight:normal;
	}
	body p{
		margin:0px 12px 12px 12px;
		padding:0;	
		font-size:10px;
		color:#666666;
		width:450px;
	}
	#head a{
		color:#006699;
		text-decoration:none;
	}
	#head a:hover{
		text-decoration:underline;
	}	
	#mainNav{
		margin:0;
		padding:10px;
	}
	#mainNav li{
		margin:0;
		padding:4px;
		list-style-type:none;
		font-size:12px;
	}
	#mainNav a{
		color:#333333;
		text-decoration:none;
	}
	#mainNav a:hover{
		text-decoration:underline;
	}
	#mainNav span{
		color:#006699;
	}
	span.location{
		color:#336600;
		font-size:12px;
	}
	
	#directoryNav{
		margin:0;
		padding:10px;
		font-size:10px;
		color:#333333;
	}
	#directoryNav li{
		margin:0;
		padding:4px;
		list-style-type:decimal;
		list-style-position:inside;
	}
	#directoryNav a{
		/*color:#333333;*/
		text-decoration:none;
		font-size:12px;
	}
	#directoryNav a:hover{
		text-decoration:underline;
	}
	.a_file{
		color:#CC3366;
		font-size:12px;
	}
	.a_dir{
		color:#FF6600;
		font-size:12px;
	}
-->
</style>

</head>

<body>



<ul id="directoryNav"><?php echo breadCrumb($directoryName); echo listDirContents($directoryName);  ?></ul>



</body>
</html>
hope it's helpful.
User avatar
ed209
Forum Contributor
Posts: 153
Joined: Thu May 12, 2005 5:06 am
Location: UK

Post by ed209 »

p.s.

If you want to browse up, just remove the '..' from the '$dontShow' array. Don't make this script accessible to the public though as there are security risks that come with that.
sheepz
Forum Commoner
Posts: 58
Joined: Wed Jul 06, 2005 11:35 pm

Post by sheepz »

thx for the script info, gonna check it out later when i get home.
Post Reply