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
Release: 2016-07-29 09:06:13
Original
1803 people have browsed it
<?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;
 }
?>
Copy after login

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>';
 }
Copy after login

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>
Copy after login
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template