Home >Backend Development >PHP Tutorial >php gets the file names of all files in a specified folder
<?php function searchDir($path,&$data){ if(is_dir($path)){ $dp=dir($path); while($file=$dp->read()){ if($file!='.'&& $file!='..'){ searchDir($path.'/'.$file,$data); } } $dp->close(); } if(is_file($path)){ $data[]=$path; } } function getDir($dir){ $data=array(); searchDir($dir,$data); return $data; } ?>
After this method is executed, it will return an array of file names. Just traverse the output:
$list=getDir('image'); for($i=0;$i<count echo></count>'; }
It can be seen from the way that the file name obtained contains the path we specified. If only To get the file name, you only need to use a for loop in getDir($dir).
Add the following code before return:
for($i=0;$i<count><br><span>In addition, if your file name is Chinese, if the display is garbled, just change the encoding method to gb2312</span><br> <p> The above introduces how PHP can obtain the file names of all files in a specified folder, including the content. I hope it will be helpful to friends who are interested in PHP tutorials. </p> <p> </p></count>