Cara menggunakan php directory listing

Cara menggunakan php directory listing



Directory Lister is the easiest way to expose the contents of any web-accessible folder for browsing and sharing. With a zero configuration, drag-and-drop installation you'll be up and running in less than a minute.

Directory Lister is created and maintained by Chris Kankiewicz (@PHLAK) with help from community contributions.

Features

  • Simple installation allows you to be up and running in less than a minute.
  • Light and dark themes to suit your professional needs or personal style.
  • Custom sort ordering gives you control of the ordering of your files/folders.
  • File search helps you locate the files you need quickly and efficiently.
  • File hashes instill confidence when downloading files through verification.
  • Readme rendering allows exposing the contents of READMEs directly on the page.
  • Zip downloads for fetching an entire directory of files in a single action.
  • Multi-lingual support brings Directory Lister to the language of your choice.

Requirements

  • Directory Lister requires PHP >= 7.4
    • The Zip extension is required for zip downloads
    • The DOM and Fileinfo extensions are required for README rendering

Installation

  1. Download Directory Lister
  2. Extract the zip/tar archive
  3. Copy extracted files/folders to your web server

Configuration

  1. Copy .env.example to .env
  2. Edit the configuration values in .env

See the Configuration Documentation for more information.

Sponsors

Love Directory Lister? Sponsor development and have your name or logo featured here!

Changelog

A list of changes can be found on the GitHub Releases page.

Troubleshooting

See the Help & Support section of the documentation for troubleshooting instructions.

For general help and support join our GitHub Discussion or reach out on Twitter.

Please report bugs to the GitHub Issue Tracker.

Copyright

This project is licensed under the MIT License.

A common request is to be able to produce a list of some or all of the files in a directory - similar to the default index page provided by most web servers, but with more control over the content and formatting.

Table of Contents

  • Single Directory Listing
  • Displaying File List in HTML
  • Recursive Directory Listing
  • Limited Depth Recursion
  • Fileinfo replaces mime_content_type
  • How can I get all filenames in a directory in PHP?
  • How can I get a list of files in a folder?
  • How do I view a directory in PHP?
  • How can I get a list of all the subfolders and files present in a directory using PHP?

Another goal might be to take some action on the files using PHP. In either case the first step is to query the file system to return a list of directories and files.

The functions presented below provide the means to extract the file names and other properties from a single directory, or to explore subdirectories recursively.

PHP 5 introduces the scandir function which will "List files and directories inside the specified path" but won't recurse or provide additional information about the listed files.

If you are using PHP 5 or higher check out our new article on Directory Listing using SPL classes for a much neater approach, and our new FileList PHP class.

Single Directory Listing

To get started, here is a simple function that returns a list of files, directories and their properties from a single directory (more advanced versions of this function can be found further down the page):

<?PHP function getFileList($dir) { // array to hold return value $retval = []; // add trailing slash if missing if(substr($dir, -1) != "/") { $dir .= "/"; } // open pointer to directory and read list of files $d = @dir($dir) or die("getFileList: Failed opening directory {$dir} for reading"); while(FALSE !== ($entry = $d->read())) { // skip hidden files if($entry{0} == ".") continue; if(is_dir("{$dir}{$entry}")) { $retval[] = [ 'name' => "{$dir}{$entry}/", 'type' => filetype("{$dir}{$entry}"), 'size' => 0, 'lastmod' => filemtime("{$dir}{$entry}") ]; } elseif(is_readable("{$dir}{$entry}")) { $retval[] = [ 'name' => "{$dir}{$entry}", 'type' => mime_content_type("{$dir}{$entry}"), 'size' => filesize("{$dir}{$entry}"), 'lastmod' => filemtime("{$dir}{$entry}") ]; } } $d->close(); return $retval; } ?>

You can use this function as follows:

<?PHP // list files in the current directory $dirlist = getFileList("."); $dirlist = getFileList("./"); // a subdirectory of the current directory called images $dirlist = getFileList("images"); $dirlist = getFileList("images/"); $dirlist = getFileList("./images"); $dirlist = getFileList("./images/"); // using an absolute path $dirlist = getFileList("{$_SERVER['DOCUMENT_ROOT']}/images"); $dirlist = getFileList("{$_SERVER['DOCUMENT_ROOT']}/images/"); ?>

The variable $_SERVER['DOCUMENT_ROOT'] should resolve to the root directory of your website. e.g. /var/www/public_html

The return value is an associative array of files including the filepath, type, size and last modified date, except when a file is actually a directory, in that case the string "(dir)" appears instead of the filesize. The filenames take the same stem as the function call:

Example 1:

<?PHP $dirlist = getFileList("images"); echo "<pre>",print_r($dirlist),"</pre>"; /* sample output Array ( [0] => Array ( [name] => images/background0.jpg [type] => image/jpeg [size] => 86920 [lastmod] => 1077461701 ) [1] => ... ) */ ?>

Example 2:

<?PHP $dirlist = getFileList("./images"); echo "<pre>",print_r($dirlist),"</pre>"; /* sample output Array ( [0] => Array ( [name] => ./images/background0.jpg [type] => image/jpeg [size] => 86920 [lastmod] => 1077461701 ) [1] => ... ) */ ?>

If you want the output sorted by one or more fields, you should read the article on Sorting Arrays of Arrays or try out one of our DHTML Sorting Algorithms using JavaScript.

We also have an article on Directory Listing using SPL classes (DirectoryIterator and SplFileInfo) which introduces many new options for filtering and sorting the output.

Displaying File List in HTML

To output the results to an HTML page we just loop through the returned array:

<?PHP // output file list in HTML TABLE format echo "<table border=\"1\">\n"; echo "<thead>\n"; echo "<tr><th>Name</th><th>Type</th><th>Size</th><th>Last Modified</th></tr>\n"; echo "</thead>\n"; echo "<tbody>\n"; foreach($dirlist as $file) { echo "<tr>\n"; echo "<td>{$file['name']}</td>\n"; echo "<td>{$file['type']}</td>\n"; echo "<td>{$file['size']}</td>\n"; echo "<td>",date('r', $file['lastmod']),"</td>\n"; echo "</tr>\n"; } echo "</tbody>\n"; echo "</table>\n\n"; ?>

This code can be easily modified to: make the output a list instead of a table; make the file names actual links; replace the names with icons based on file type or extension; etc.

Display PNG images in a TABLE:

For example, to display only PNG files, just add a condition to the output loop:

<?PHP // output file list as HTML table echo "<table border=\"1\">\n"; echo "<thead>\n"; echo "<tr><th></th><th>Name</th><th>Type</th><th>Size</th><th>Last Modified</th></tr>\n"; echo "</thead>\n"; echo "<tbody>\n"; foreach($dirlist as $file) { if(!preg_match("/\.png$/", $file['name'])) { continue; } echo "<tr>\n"; echo "<td><img src=\"{$file['name']}\" width=\"64\" alt=\"\"></td>\n"; echo "<td>{$file['name']}</td>\n"; echo "<td>{$file['type']}</td>\n"; echo "<td>{$file['size']}</td>\n"; echo "<td>",date('r', $file['lastmod']),"</td>\n"; echo "</tr>\n"; } echo "</tbody>\n"; echo "</table>\n\n"; ?>

Here you can view the complete source code for this example.

This will have the effect of skipping all files whose name does not end with .png. You could also apply conditions based on the file type, size, or last modified timestamp.

List PDF files with links:

One last example, listing only PDF files and having the file name link to the file:

<table border="1"> <thead> <tr><th>Name</th><th>Type</th><th>Size</th><th>Last Modified</th></tr> </thead> <tbody> <?PHP // output file list as table rows foreach($dirlist as $file) { if($file['type'] != 'application/pdf') { continue; } echo "<tr>\n"; echo "<td><a href=\"{$file['name']}\">",basename($file['name']),"</a></td>\n"; echo "<td>{$file['type']}</td>\n"; echo "<td>{$file['size']}</td>\n"; echo "<td>",date('r', $file['lastmod']),"</td>\n"; echo "</tr>\n"; } ?> </tbody> </table>

Here you can view the complete source code for this example.

If you want to display, for example, a thumbnail as a link to a larger image, or even a video, just give the two files the same name and in the script above use str_replace or similar function to modify either the link href or the link contents. See our article on listing images for examples.

Using the SPL DirectoryIterator and FilterIterator classes we can now specify a pattern to match when accessing the file list so only matching files are returned. More on that here.

Recursive Directory Listing

Now that we've got this far, it's only a minor change to extend the function in order to recursively list any subdirectories. By adding a second parameter to the function we also retain the previous functionality of listing a single directory.

<?PHP // Original PHP code by Chirp Internet: www.chirpinternet.eu // Please acknowledge use of this code by including this header. function getFileList($dir, $recurse = FALSE) { $retval = []; // add trailing slash if missing if(substr($dir, -1) != "/") { $dir .= "/"; } // open pointer to directory and read list of files $d = @dir($dir) or die("getFileList: Failed opening directory {$dir} for reading"); while(FALSE !== ($entry = $d->read())) { // skip hidden files if($entry{0} == ".") continue; if(is_dir("{$dir}{$entry}")) { $retval[] = [ 'name' => "{$dir}{$entry}/", 'type' => filetype("{$dir}{$entry}"), 'size' => 0, 'lastmod' => filemtime("{$dir}{$entry}") ]; if($recurse && is_readable("{$dir}{$entry}/")) { $retval = array_merge($retval, getFileList("{$dir}{$entry}/", TRUE)); } } elseif(is_readable("{$dir}{$entry}")) { $retval[] = [ 'name' => "{$dir}{$entry}", 'type' => mime_content_type("{$dir}{$entry}"), 'size' => filesize("{$dir}{$entry}"), 'lastmod' => filemtime("{$dir}{$entry}") ]; } } $d->close(); return $retval; } ?>

To make use of the new functionality, you need to pass a value of TRUE (or 1) as the second parameter.

<?PHP // single directory $dirlist = getFileList("./"); // include subdirectories $dirlist = getFileList("./", TRUE); ?>

Before recursing the script first checks whether sub-directories are readable, and otherwise moves on to the next item so as to avoid permission errors.

As before, the return value is an array of associative arrays. In fact the only change is that you have the additional option of a recursive listing.

Limited Depth Recursion

This final example adds another feature - the ability to specify how deep you want the recursion to go. The previous code would continue to explore directories until it ran out of places to go. With this script you can tell it to not go deeper than a fixed number of levels in the file system.

<?PHP // Original PHP code by Chirp Internet: www.chirpinternet.eu // Please acknowledge use of this code by including this header. function getFileList($dir, $recurse = FALSE, $depth = FALSE) { $retval = []; // add trailing slash if missing if(substr($dir, -1) != "/") { $dir .= "/"; } // open pointer to directory and read list of files $d = @dir($dir) or die("getFileList: Failed opening directory {$dir} for reading"); while(FALSE !== ($entry = $d->read())) { // skip hidden files if($entry{0} == ".") continue; if(is_dir("{$dir}{$entry}")) { $retval[] = [ 'name' => "{$dir}{$entry}/", 'type' => filetype("{$dir}{$entry}"), 'size' => 0, 'lastmod' => filemtime("{$dir}{$entry}") ]; if($recurse && is_readable("{$dir}{$entry}/")) { if($depth === FALSE) { $retval = array_merge($retval, getFileList("{$dir}{$entry}/", TRUE)); } elseif($depth > 0) { $retval = array_merge($retval, getFileList("{$dir}{$entry}/", TRUE, $depth-1)); } } } elseif(is_readable("{$dir}{$entry}")) { $retval[] = [ 'name' => "{$dir}{$entry}", 'type' => mime_content_type("{$dir}{$entry}"), 'size' => filesize("{$dir}{$entry}"), 'lastmod' => filemtime("{$dir}{$entry}") ]; } } $d->close(); return $retval; } ?>

As before we've added a single new parameter and a few lines of code. The default value of the depth parameter, if not defined in the function call, is set to FALSE. This ensures that all previous features remain and that any legacy code won't break when the function is changed.

In other words, we can now call the getFileList function with one, two or three parameters:

<?PHP // single directory $dirlist = getFileList("./"); // include all subdirectories recursively $dirlist = getFileList("./", TRUE); // include just one or two levels of subdirectories $dirlist = getFileList("./", TRUE, 1); $dirlist = getFileList("./", TRUE, 2); ?>

This is a good example of how a function can evolve over time without becoming unmanageable. Too often you see functions that were once useful become unusable because of parameter bloat.

Fileinfo replaces mime_content_type

The mime_content_type function has been deprecated in recent version of PHP and replaced with the PECL Fileinfo extension was never actually deprecated, despite announcements to the contrary.

If you are seeing errors in your program due to this, the following patch should work:

<?PHP function getFileList($dir) { if(function_exists('mime_content_type')) $finfo = FALSE; } else { $finfo = finfo_open(FILEINFO_MIME_TYPE); } // array to hold return value $retval = []; // add trailing slash if missing if(substr($dir, -1) != "/") { $dir .= "/"; } // open pointer to directory and read list of files $d = @dir($dir) or die("getFileList: Failed opening directory {$dir} for reading"); while(FALSE !== ($entry = $d->read())) { // skip hidden files if($entry{0} == ".") continue; if(is_dir("{$dir}{$entry}")) { $retval[] = [ 'name' => "{$dir}{$entry}/", 'type' => filetype("{$dir}{$entry}"), 'size' => 0, 'lastmod' => filemtime("{$dir}{$entry}") ]; } elseif(is_readable("{$dir}{$entry}")) { $retval[] = [ 'name' => "{$dir}{$entry}", 'type' => ($finfo) ? finfo_file($finfo, "{$dir}{$entry}") : mime_content_type("{$dir}{$entry}"), 'size' => filesize("{$dir}{$entry}"), 'lastmod' => filemtime("{$dir}{$entry}") ]; } } $d->close(); return $retval; } ?>

This is the same function as seen at the top of the page, just with a check for the mime_content_type function inserted at the top to determine which method we use to get the file type.

If you are scanning a complex directory system you probably want to declare $finfo as a global variable to avoid having to re-instantiate it when the function recurses.

References

  • PHP: Directory Functions
  • PHP: Array Functions
  • PHP Directory Listing
  • PHP Directory Listing: Images
  • PHP Directory Listing using SPL
  • PHP File Listing Class

< PHP

Send a message to The Art of Web:

press <Esc> or click outside this box to close

Post your comment or question

How can I get all filenames in a directory in PHP?

Approach: In order to get all the files from the particular directory, we need to specify the complete path of the file & store the path value in the variable as $mydir. Then use the scandir() function that will scan for the files in a current or specific directory & return an array of files and directories.

How can I get a list of files in a folder?

Press and hold the SHIFT key and then right-click the folder that contains the files you need listed. Click Open command window here on the new menu. A new window with white text on a black background should appear. o To the left of the blinking cursor you will see the folder path you selected in the previous step.

How do I view a directory in PHP?

PHP scandir() Function $b = scandir($dir,1);

How can I get a list of all the subfolders and files present in a directory using PHP?

PHP using scandir() to find folders in a directory To check if a folder or a file is in use, the function is_dir() or is_file() can be used. The scandir function is an inbuilt function that returns an array of files and directories of a specific directory.