• 0

php list directory script


Question

hi!

simple question - not so simple answer... :(

well i have a directory on my webserver where i keep my projects...

i want to make a page where with the help of php script would list all the directory that exist in the folder automatically without having to edit the code and make a link so i can see them.

how can i do this?

Link to comment
https://www.neowin.net/forum/topic/80831-php-list-directory-script/
Share on other sites

16 answers to this question

Recommended Posts

  • 0

nice tutorials!!!!!!!!!! thanks man... here is the code with a little change i made :

<?
$path = "/my/path/";
$dir_handle = @opendir($path) or die("unable to open $path");
while ($file = readdir($dir_handle)) {
	if($file == "." || $file == ".." || $file == "*.*" )
	continue;
	echo "<a href=$file target=\"_blank\">$file</a><br>";
}
closedir($dir_handle);
?>

now the question...

i don't want to display ANY file...only the directories...

but this :

if($file == "." || $file == ".." || $file == "*.*" )
continue;

won't do it!

why?

  • 0

yeah, wild card symbols can mess up your script. Also be carefull how you use this because those function can be huge security holes. So make sure to limit each variable to only directories you want with the possibility to list. using "==" is the perfect solution. :p

goodluck with your scripting ^_^

  • 0

i made a sorta code viewer / directory parser using this code.

<?PHP


//it connects to the db above here and pulls a user id from the link. If there is no ID > login again, if the id is there but there is no admin then returns a bad error.
if( $id=="") { echo"<font face='verdana' size='1'>error; please <a href='index.php'>re-login.</a>";}
elseif($row[admin]!="1"){echo"<font face='verdana' size='1'>Error: You do not have sufficient permissions to access this page. Please login again.";
//this inserts the time, the action, id, the name, and ip into a log database
  $timestamp=(gmstrftime("%b %d %Y %H:%M:%S",time()+3600));
$lquery = "INSERT INTO log VALUES ('','$timestamp','Illegal Dir access attempted','$row[realname]','$id','$REMOTE_ADDR')";
 $lresult = mysql_query ($lquery);
}

//gets the file listing from the dir
else {

if (!isset($action)) {
  $timestamp=(gmstrftime("%b %d %Y %H:%M:%S",time()+3600));
$lquery = "INSERT INTO log VALUES ('','$timestamp','Directory Viewer Accessed','$row[realname]','$id','$REMOTE_ADDR')"; // logs the dir file access
 $lresult = mysql_query ($lquery);

//define the path as relative

$path = "patj";
//using the opendir function
$dir_handle = @opendir($path) or die("Unable to open $path");
echo"<html>\n<head><title>Directory Listing for $path</title></head><body><font face='verdana' size='1'>";
echo "Directory Listing of $path<BR>";
//running the while loop
while ($file = readdir($dir_handle)) {
        echo "<a href=$file>$file</a> (<a href=$PHP_SELF?action=code&file=$file&id=$id>code</a>)<br>";
//so i built the link up with Id, file, and an action code.
}
//closing the directory
closedir($dir_handle); }

else { 
//quick user check...
	if( $id=="") { echo"<font face='verdana' size='1'>error; please <a href='index.php'>re-login.</a>";}
elseif($row[admin]!="1"){echo"<font face='verdana' size='1'>Error: You do not have sufficient permissions to access this page. Please login again."; }
else{
//it will show you all the code via a file called xxxxxx.php = a file which contains special nfo...
  if($action=="code") {	if($file=="xxxxxx.php") {echo"ERROR";} else {  show_source($file);  
    $timestamp=(gmstrftime("%b %d %Y %H:%M:%S",time()+3600));
$lquery = "INSERT INTO log VALUES ('','$timestamp','Code for $file viewed','$row[realname]','$id','$REMOTE_ADDR')";
 $lresult = mysql_query ($lquery); // again log the action
  }
	}
}
}

}
echo"<br><B><font face='verdana' size='1'><a href=index2.php?id=$id>Home</a> | NOTE : All views of this page are logged, with your IP."; // return the user home and a nice warning... 
?>

  • 0

hmmm...nice code mr magoo but we can use that only if we have the same template as you do...

for example postnuke or sth...

if not... then we gotta change a lot of thing there! :)

well...i'm making now a remote file management script where you can upload, download, view files and is indepentend of any php portal template...

that would be great!...if i make it! :D

  • 0

i dont think so :)

you can deploy that in any directory you want.

<?PHP
/******************************************************
Toms Directory & code parser script! Upload,
complete the required variables, and it should 
work. Possible additions could be logging of actions,
or a user validation system. It would be unwise
if one were to use this without any protection.
******************************************************/
/*variables */

$path = "path/to/directory";	//relative directory path

//if not action > parse directory; the action for viewing the file source is "code"
if (!isset($action)) {
//using the opendir function
$dir_handle = @opendir($path) or die("Unable to open $path");
//send out page title
echo"<html>\n<head><title>Directory Listing for $path</title></head><body><font face='verdana' size='1'>";
echo "Directory Listing of $path<BR>";
//running the while loop
while ($file = readdir($dir_handle)) {
       echo "<a href=$file>$file</a> (<a href=$PHP_SELF?action=code&file=$file>code</a>)<br>";
//so i built the link up with Id, file, and an action code.
}
//closing the directory
closedir($dir_handle); 
}

else{
//it will show you all the code via a file called xxxxxx.php = a file which contains special nfo...
 if($action=="code") { if($file=="xxxxxx.php") {echo"ERROR";} else {  echo"<font face=verdana size=1><b>Code for $file</b> :<font size=3 face=tahoma><br />";show_source($file);   }
}

}

echo"<br><B><font face='verdana' size='1'><a href=returnhomelink.html>Home</a> | NOTE : All views of this page are logged, with your IP."; 

// return the user home and a nice warning...
?>

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.