List all files in one directory PHP

Shadowpat picture Shadowpat · Apr 2, 2013 · Viewed 545.3k times · Source

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!

Answer

miah picture miah · Apr 2, 2013

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('.', '..'));