Php – How to read a list of files from a folder using PHP?


I want to read a list the names of files in a folder in a web page using php.
is there any simple script to acheive it?

Best Solution

The simplest and most fun way (imo) is glob

foreach (glob("*.*") as $filename) {
    echo $filename."<br />";

But the standard way is to use the directory functions.

if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {
            echo "filename: .".$file."<br />";

There are also the SPL DirectoryIterator methods. If you are interested