• 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.
  • Posts

    • eh I'll wait for the June 2026 MVS ISO downloads which should be coming out next Tuesday June 16 and possibly contain build 8655 instead of 8653
    • read this recent topic in another forum: https://www.askwoody.com/forums/topic/still-on-win-10-and-happy-to-be-there/ some people are happy sticking with Win10
    • Cooler Master MasterFrame 600 PC case is now 33% off on Amazon by Ivan Jenic The Cooler Master MasterFrame 600 is currently $109.99 on Amazon, down from its original $164.99 list price. That's 33% off and $55 saved on this premium aluminum mid-tower case with a modular design. If you're upgrading your PC case and want something that doesn't force you into a rigid layout, the MasterFrame 600 is worth a look. The case is built around the Cooler Master's FreeForm 2.0 platform, which lets you reconfigure the internal structure according to your hardware. Magnetic side panels allow for straightforward adjustments, and the case supports everything from Mini-ITX to E-ATX motherboards without compromise. There's also generous cooling headroom. Four pre-installed PWM fans handle airflow out of the box. GPU clearance goes up to 410mm, and the case supports radiators up to 420mm with room for three simultaneously. Truth be told, this might not be the prettiest case on the market, but it’s highly functional. The aluminum construction keeps the whole thing lightweight despite its size, and the finish looks noticeably better than the plastic mid-towers competing at this price point. If you want a serious, flexible case that prioritizes function over flashy aesthetics like RGB lighting, the MasterFrame 600 delivers at a reasonable price. Cooler Master MasterFrame 600 - $109.99 | 33% off on Amazon This Amazon deal is US-specific and not available in other regions unless specified. This is a first-party seller link (at the time of article publishing); ensure that you also purchase from a first-party seller link only. If you don't like it or want to look at more options, check out the previous deals that we have covered, OR you can also visit Amazon US deals page. Get Prime (SNAP), Prime Video, Audible Plus or Kindle / Music Unlimited. Free for 30 days. As an Amazon Associate, we earn from qualifying purchases.
    • DK, I don't use the extended channel, I'm always on the latest release.
  • Recent Achievements

    • Rookie
      restore went up a rank
      Rookie
    • Very Popular
      AndrewSteel earned a badge
      Very Popular
    • Veteran
      Taliseian went up a rank
      Veteran
    • One Month Later
      Clizby earned a badge
      One Month Later
    • One Month Later
      Timaximus earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      509
    2. 2
      +Edouard
      162
    3. 3
      PsYcHoKiLLa
      155
    4. 4
      ATLien_0
      82
    5. 5
      Steven P.
      79
  • Tell a friend

    Love Neowin? Tell a friend!