Home>Article>Backend Development> How to read all file names in php
php gets all the file names in the directory
1. Open the directory handle of the directory to be operated
Code example:
//打开当前目录下的目录pic下的子目录common。 $handler = opendir('pic/common');
2. Loop through all files in the directory
Code example:
/*其中$filename = readdir($handler) 每次循环时将读取的文件名赋值给$filename,$filename !== false。 一定要用!==,因为如果某个文件名如果叫'0′,或某些被系统认为是代表false,用!=就会停止循环 */ while( ($filename = readdir($handler)) !== false ) { //略过linux目录的名字为'.'和‘..'的文件 if($filename != “.” && $filename != “..”) { //输出文件名 echo $filename; } }
3. Close the directory
Code sample:
closedir($handler);
Recommended tutorial:PHP video tutorial
The above is the detailed content of How to read all file names in php. For more information, please follow other related articles on the PHP Chinese website!