How to list all files in a folder using PHP?

To list all the files in a folder, you can use the scandir() function in PHP. It returns an array that contains the names of the files and directories in the specified folder.

Here is an example:

$dir = 'path/to/folder'; // 文件夹的路径

$files = scandir($dir); // 列出文件夹中的所有文件和目录

foreach($files as $file){
    if($file != '.' && $file != '..'){
        echo $file . '<br>'; // 输出文件和目录的名称
    }
}

Please make sure to replace the $dir variable with the path of the folder you want to list files from. In the foreach loop, we use a conditional statement to exclude special directories (such as “.” and “..”).

bannerAds