Getting the names of all files in a directory with PHP
In PHP, you can Example of using the scandir() function to get the names of all files in a directory in PHP. The function returns an array of filenames, which you can then loop through to access each file. Here is an example of how to use the scandir()
function to get the names of all files in a directory:
<?php
$directory = '/path/to/directory';
$files = scandir($directory);
foreach ($files as $file) {
echo $file . '<br>';
}
Watch a video course
Learn object oriented PHP
Alternatively, you can use glob function
<?php
foreach (glob('/path/to/directory/*') as $file) {
echo $file . '<br>';
}
You can also use the opendir()
, readdir()
, and closedir()
functions to read the contents of a directory, but scandir()
is simpler and more efficient.