PHP: Display directory contents


For security reasons, most servers on the Internet do not show the contents of a directory if the directory address is typed. The user is either redirected to index.whatever, a default page, page not found or access denied. Thus the list of files in the directory are not displayed.

If you need to show the contents of your directory, you can use the following code:

exec("ls", $output, $return);
foreach ($output as $file ) {
   if ($file != "index.php") { 
      print "<a href='$file'>$file</a><br>"; 
   }
}

If exec() is blocked by your server due to SELinux settings, you can use proc_open() instead.