Home >Backend Development >PHP Tutorial >PHP gets all files in a directory and saves the results to an array (instance)_PHP tutorial

PHP gets all files in a directory and saves the results to an array (instance)_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:25:59980browse

At work, we sometimes encounter operations that require batch processing of files in a certain directory. At this time, we need to find all the qualified files in the directory and save them into a result set, and then facilitate batch processing. Usually The method is to save it into an array and then process it in a loop. The process will be recorded below.

Copy the code The code is as follows:

//php gets all the files in the directory and saves the results To the array
foreach(glob("./*") as $d){
$tmp=explode('.',$d);
$k=end($tmp);
//If it is a file, and the file extension is jpg png
if(is_file($d)&&in_array($k,array('jpg','png'))){
$files[] =$d;
}
}
echo '
';print_r($files);

The above source code lists all the suffixes in the current directory. jpg png file, save the result as an array and then print it out. The results of this test are printed as follows:
PHP gets all files in a directory and saves the results to an array (instance)_PHP tutorial

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/824871.htmlTechArticleAt work, sometimes we encounter the need to batch process files in a certain directory. At this time, we will You need to find all the files that meet the conditions in this directory and save them to a...
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