Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List all files in one directory PHP [duplicate]

What would be the best way to list all the files in one directory with PHP? Is there a $_SERVER function to do this? I would like to list all the files in the usernames/ directory and loop over that result with a link, so that I can just click the hyperlink of the filename to get there. Thanks!

like image 654
Shadowpat Avatar asked Apr 02 '13 21:04

Shadowpat


2 Answers

You are looking for the command scandir.

$path    = '/tmp';
$files = scandir($path);

Following code will remove . and .. from the returned array from scandir:

$files = array_diff(scandir($path), array('.', '..'));
like image 177
miah Avatar answered Sep 25 '22 02:09

miah


Check this out : readdir()

This bit of code should list all entries in a certain directory:

if ($handle = opendir('.')) {

    while (false !== ($entry = readdir($handle))) {

        if ($entry != "." && $entry != "..") {

            echo "$entry\n";
        }
    }

    closedir($handle);
}

Edit: miah's solution is much more elegant than mine, you should use his solution instead.

like image 23
Orel Biton Avatar answered Sep 24 '22 02:09

Orel Biton