Home >Backend Development >PHP Tutorial >php gets the file names of all files in a specified folder

php gets the file names of all files in a specified folder

WBOY
WBOYOriginal
2016-07-29 09:06:131811browse
<?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>';
 }

Output result:

php gets the file names of all files in a specified folder

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>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn