Jump to content

Directory Listing


Warptweet

Recommended Posts

Here is my code for listing all files in a directory...

[code]<?

//define the path as relative
$path = "/home/yoursite/public_html/whatever";

//using the opendir function
$dir_handle = @opendir($path) or die("Unable to open $path");

echo "Directory Listing of $path<br/>";

//running the while loop
while ($file = readdir($dir_handle))
{
  echo "<a href='$file'>$file</a><br/>";
}

//closing the directory
closedir($dir_handle);

?>
[/code]

How can I...

A)Only show .php files
B)Hide the .php part in the file name from viewers.
Link to comment
https://forums.phpfreaks.com/topic/34824-directory-listing/
Share on other sites

In here...

while ($file = readdir($dir_handle))
{
  echo "<a href='$file'>$file</a><br/>";
}

change to

[code=php:0]
while($file = readdir($dir_handle))
{
  if(substr($file, -4) == '.php')
  {
      $length = strlen($file);
      echo substr($file, 0, ($length-4));
  }
}
[/code]

Or that should be close at least... I didn't test.
Link to comment
https://forums.phpfreaks.com/topic/34824-directory-listing/#findComment-164151
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.