Home  >  Article  >  Backend Development  >  PHP gets all files in a directory and saves all directories to array_PHP tutorial

PHP gets all files in a directory and saves all directories to array_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:01:311020browse

PHP gets all the files and directories in the directory and saves them into an array. This is a program that uses the glob function to save all the files and folders in the specified directory into data. Then we use foreach to judge and save them into the relevant data. ​

php tutorial to get all files in a directory and save all directories to array program
This is a method that uses the glob function to save all the files and folders in the specified directory to data, and then we use foreach to judge and save them into the relevant data.

$dirs = array();
foreach(glob("./*") as $d)
{
$tmp = explode('.',$d);
$k = end($tmp);
If(is_file($d) && $k =='php')
{
          $dirs[]                                                                                                            }
}
print_r($dirs);
//Show all directories in the directory

$v =array();
foreach(glob("./*") as $vv)
{
// $tmp = explode('.',$d);
//$k = end($tmp);
If(is_dir($vv)) //The difference between directories and files is to use is_dir or is_file to judge
{
         $v[] = $vv;
}
}
print_r($v);
/*

Result

array

(
[0] => ./1.php
[1] => ./10.php
[2] => ./11.php
[3] => ./3.php
[4] => ./4.php
[5] => ./5.php
[6] => ./7.php
[7] => ./8.php
[8] => ./9.php
)

array

(
[0] => ./www.bkjia.com
)
*/


http://www.bkjia.com/PHPjc/445444.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/445444.htmlTechArticlephp gets all files in a directory and saves all directories to an array. This is a program that uses the glob function to save all files in a specified directory. Files and folders are saved to data, and then we use foreach to judge...
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