Home > PHP > PHP: Display hyperlinked list of files in a directory

PHP: Display hyperlinked list of files in a directory

The code below will display a hyperlinked list of all the files contained in a specified folder. This is a very simple way to have your own directory browser. Simply copy the code below into a new notepad document and save it as browser.php. Now upload the file to somewhere on your web server and browse to it. You should see an index of all files in the directory of your webserver.

<?php
$path = “/home/username/public_html/folder/”;

$dir_handle = @opendir($path) or die(“Unable to open $path”);

while ($file = readdir($dir_handle)) {
if(
$file == “.” || $file == “..” || $file == “index.php” )
continue;
echo 
“<a href=\”$file\”>$file</a><br />”;
}

closedir($dir_handle);

?>

And there you have it! A very simple way of creating a directory browser.

Categories: PHP Tags:
  1. No comments yet.
  1. No trackbacks yet.