• 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

    • Because of the EU (a good thing) newer android devices been getting 5 years worth of security patches. Except some Motorola which found the loop hole, and offer ZERO updates. In addition, Google for years have been making where it can patch some stuff by updating the core Google Play Store itself.  As echoed earlier,  you take the security risk in to your own hand beyond supported.
    • Win11Debloat 06.11.2026 by Razvan Serea Win11Debloat is a lightweight, easy to use PowerShell script that allows you to quickly declutter and customize your Windows experience. It can remove pre-installed bloatware apps, disable telemetry, remove intrusive interface elements and much more. The script also includes many features that system administrators and power users will enjoy. Such as a powerful command-line interface, support for Windows Audit mode and the option to make changes to other Windows users. All changes made by Win11Debloat can be easily reversed, and most removed apps can be restored via the Microsoft Store. A full guide on how to undo the changes is available here. Win11Debloat features: Below is an overview of the key features and functionality offered by Win11Debloat. Please refer to the wiki for more information about the default settings preset. Remove a wide variety of preinstalled apps. Click here for more info. Disable telemetry, diagnostic data, activity history, app-launch tracking & targeted ads. Disable tips, tricks, suggestions & ads across Windows. Disable Windows location services & app location access. Disable Find My Device location tracking. Disable 'Windows Spotlight' and tips & tricks on the lock screen. Disable 'Windows Spotlight' desktop background option. Disable ads, suggestions and the MSN news feed in Microsoft Edge. Hide Microsoft 365 ads on the Settings 'Home' page, or hide the 'Home' page entirely. Disable & remove Microsoft Copilot. Disable Windows Recall. Disable Click to Do, AI text & image analysis tool. Prevent AI service (WSAIFabricSvc) from starting automatically. Disable AI Features in Edge. Disable AI Features in Paint. Disable AI Features in Notepad. Disable the Drag Tray for sharing & moving files. Restore the old Windows 10 style context menu. Turn off Enhance Pointer Precision, also known as mouse acceleration. Disable the Sticky Keys keyboard shortcut. Disable Storage Sense automatic disk cleanup. Disable fast start-up to ensure a full shutdown. ...and more. Once you’ve downloaded the Win11Debloat file (Get.ps1), just follow these quick steps: Locate the Get.ps1 script file. Right-click the file and select Run with PowerShell from the context menu. If prompted by User Account Control (UAC), select Yes to grant the script the necessary administrative permissions. Win11Debloat 06.11.2026 fixes: Fix lock screen spotlight option being disabled when disabling the start recommended section by @Raphire in #619 Fix log message formatting by @Raphire Note The -RemoveCommApps and -RemoveW11Outlook command-line parameters for uninstalling a few specific apps have been removed with this release. If you previously relied on these parameters, please see this wiki page for alternative methods of removing these apps. Download: Win11Debloat 06.11.2026 | Open Source View: Win11Debloat Home Page | Screenshots 1| 2 Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Yes for me, I installed 'old calculator' (Windows 7 calculator) in its place since it is more useful to me. I think paint is the only one I left installed
  • 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
      501
    2. 2
      +Edouard
      162
    3. 3
      PsYcHoKiLLa
      154
    4. 4
      ATLien_0
      83
    5. 5
      Steven P.
      79
  • Tell a friend

    Love Neowin? Tell a friend!