Wednesday, January 23, 2008

Display Current Directory Files

Apache usually will display the default web pages usually index.php or index.html where you had predefine in your configuration files. So how could we display other files easily? The best way to implement is that you create a file which will output all files in the directory. Below is example of how could u implement.

Save the as index.php


<html>

<?php

/**zfm
* Name this file index.php and place in the directory.
*/


//Data Initialization
$counter = 1;


// Define the full path to your folder from root
//$path = "put the full path here";
$path = "./.";

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

echo "<font size=5>Directory folder</font><br><br>";

// Loop through the files
while ($file = readdir($dir_handle)) {

if($file == "." || $file == ".." || $file == "index.php" )

continue;
echo $counter." <a href=\"$file\">$file</a><br>";

$counter+=1;

}

// Close
closedir($dir_handle);

?>
</html>

No comments: