Home > Article > Backend Development > PHP gets all files in a directory and saves all directories to array_PHP tutorial
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();//Show all directories in the directory
foreach(glob("./*") as $d)
{
$tmp = explode('.',$d);
$k = end($tmp);
If(is_file($d) && $k =='php')
{
$dirs[] }
}
print_r($dirs);
$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
(
[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
)
(
[0] => ./www.bkjia.com
)
*/