• 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

    • I bought one of these last year. I really hope I won't have to deal with this crap. My drive is in good condition at the moment.
    • Umm, read my answer again! If you have something to add or contribute, feel free. Otherwise my point was that you apparently dont want faster updates... so you want slower updates by process of elimination. If you have something to contribute, meaningful answers are better.
    • These features described above are good, but far from what developers will like the most. The main feature that developers will care and love the most it's called "Bring Your Own Models". It gives us the ability to connect to LOCAL AI models running on Ollama. The feature it's located on GitHub Copilot tab -> On the model picker where you can select "manage models" instead of paid models and then it will show you the "Bring your own models" window where you can now select Ollama and the endpoint of your local server. So if you have a beefy spec machine you can now use your own model 100% local inside Visual Studio 2026 18.7.0
    • Microsoft Teams is getting a controversial location tracking feature that users may hate by Usama Jawad Image generated with Microsoft Copilot Earlier this year, Microsoft planned to roll out a controversial location tracking feature in Teams, but following customer feedback, it decided to delay its release. The bad news is that the company has decided to launch it later this year, but it's based on roughly the same design that was shared earlier, which means that many users still have good reason to worry. Basically, Microsoft Places and Teams have received workplace check-ins via Wi-Fi. The idea is that if an employee arrives at the office and connects to their enterprise network, their profile status indicator will show them as being present in the office. For example, if you arrive at work, open Teams on your PC, and connect to the "Studio B" company Wi-Fi network, your Teams profile will indicate that you are present in "Studio B", as shown below: Microsoft says that this feature is basically a replacement for physical workplace check-in peripherals, it reduces the need to manually update your status, and it also enables co-workers to know that you're at work so that they can coordinate in-person meetings with you. IT admins can enable this workplace check-in capability at a tenant level, and users have the ability to control whether they want to enable it or not. Of course, all of that sounds great on paper, but naturally, many Teams customers may still have concerns, as they did before. This is because it enables your reporting manager and other members of the organization to track if you are at the office, when you arrive at the office, and where you are right now. This could be problematic for people who work in what they consider to be flexible work environments or hybrid setups, and this kind of location tracking could be considered an invasion of privacy. Microsoft has tried to alleviate some of these concerns by letting users know that they can manually set their location easily, which essentially overrides workplace check-in if they feel uncomfortable with it. However, that doesn't really solve the problem because your organization could enforce a workplace policy that mandates that this feature remains enabled. The Redmond tech giant has also assured users that this capability does not store historical data and is only a real-time indicator of location. Finally, it only generates a signal when you connect to a corporate network, which means that if you are working from home and connect your PC to your personal Wi-Fi, it won't broadcast your location to your employer; you will simply be shown as "Remote". Microsoft has encouraged IT admins to prepare for this change and begin informing users so they know what to expect once it begins rolling out later this year.
  • Recent Achievements

    • 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
    • Week One Done
      Timaximus earned a badge
      Week One Done
  • Popular Contributors

    1. 1
      +primortal
      516
    2. 2
      +Edouard
      162
    3. 3
      PsYcHoKiLLa
      157
    4. 4
      Steven P.
      82
    5. 5
      ATLien_0
      81
  • Tell a friend

    Love Neowin? Tell a friend!