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!
asked Apr 2, 2013 at 21:23
7
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['.', '..']];
answered Apr 2, 2013 at 21:26
miahmiah
9,6383 gold badges19 silver badges32 bronze badges
8
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.
answered Apr 2, 2013 at 21:26
Orel BitonOrel Biton
3,3782 gold badges14 silver badges14 bronze badges
4
[PHP 5, PHP 7, PHP 8]
scandir — List files and directories inside the specified path
Description
scandir[string $directory
, int $sorting_order
= SCANDIR_SORT_ASCENDING
, ?resource $context
=
null
]: array|false
Parameters
directory
The directory that will be scanned.
sorting_order
By default, the sorted order is alphabetical in ascending order. If the optional sorting_order
is set to SCANDIR_SORT_DESCENDING
, then the sort order is alphabetical in descending order. If it is set to
SCANDIR_SORT_NONE
then the result is unsorted.
context
For a description of the context
parameter, refer to the streams section of the manual.
Return Values
Returns an array of filenames on success, or false
on failure. If directory
is not a directory,
then boolean false
is returned, and an error of level E_WARNING
is generated.
Changelog
8.0.0 | context is now nullable.
|
Examples
Example #1 A simple scandir[] example
The above example will output something similar to:
Array [ [0] => . [1] => .. [2] => bar.php [3] => foo.txt [4] => somedir ] Array [ [0] => somedir [1] => foo.txt [2] => bar.php [3] => .. [4] => . ]
Notes
Tip
A URL can be used as a filename with this function if the fopen wrappers have been enabled. See fopen[] for more details on how to specify the filename. See the Supported Protocols and Wrappers for links to information about what abilities the various wrappers have, notes on their usage, and information on any predefined variables they may provide.
See Also
- opendir[] - Open directory handle
- readdir[] - Read entry from directory handle
- glob[] - Find pathnames matching a pattern
- is_dir[] - Tells whether the filename is a directory
- sort[] - Sort an array in ascending order
dwieeb at gmail dot com ¶
10 years ago
Easy way to get rid of the dots that scandir[] picks up in Linux environments:
coolbikram0 at gmail dot com ¶
10 months ago
A simple recursive function to list all files and subdirectories in a directory:
mmda dot nl at gmail dot com ¶
9 years ago
Here is my 2 cents. I wanted to create an array of my directory structure recursively. I wanted to easely access data in a certain directory using foreach. I came up with the following:
Output
Array
[
[subdir1] => Array
[
[0] => file1.txt
[subsubdir] => Array
[
[0] => file2.txt
[1] => file3.txt
]
]
[subdir2] => Array
[
[0] => file4.txt
}
]
info at remark dot no ¶
4 years ago
Someone wrote that array_slice could be used to quickly remove directory entries "." and "..". However, "-" is a valid entry that would come before those, so array_slice would remove the wrong entries.
eep2004 at ukr dot net ¶
7 years ago
Fastest way to get a list of files without dots.