List folder files in html with php

Sometimes we need to list files of some folder in our web page. This script will show you how you cam simply update your web page by adding new files in folder. It is done by use of PHP and HTML code. List files in HTML with this simple php script. 

Code:

<?php
$results = array();
 $directory = "download/";
   $i = 0; // create a handler for the directory
    $handler = opendir($directory);

    // open directory and walk through the filenames
?>
<html>
<head>
<title>List of files</title>
</head>
<body>
<div align="center">
<?php
while ($file = readdir($handler)) {

      // if file isn't this directory or its parent, add it to the results
      if ($file != "." && $file != "..") {
        $results[] = $file;

	echo '<a href="'.$directory .$file . '">'.$file . '</a> <br /> <br />';

	}}

    // tidy up: close the handler
    closedir($handler);
?>

</div>
</body>
</html>

Because our web browsers are sometimes handeling the files by opening them in them self’s wee haw to trick them to think the file is yust for download. So for every type of file wee haw to put some cod in .htaccess file like this.

<FilesMatch "\.(?i:mp3)$">
  ForceType application/octet-stream
  Header set Content-Disposition attachment
</FilesMatch>

Demo and Download of this script:

 

 

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

Captcha Captcha Reload