PHP itself has a readdir function, but it can only read the current directory. Based on this function, I wrote another function to achieve my needs. The principle of the function is very simple, mainly using recursive calls.
The code is as follows
代码如下
复制代码
class listdir{
var $depth;
var $dirname;
var $list;
var $tostring;
function listdir($dir){
$this->dirname=$dir;
$this->depth=0;
$this->tostring=”";
}
var $tostring;
function listdir($dir){
$this->dirname=$dir;
$this->depth=0;
$this->tostring=””;
}//Save the result into a multi-dimensional array
function getlist($dir=""){
if($dir==””)$dir=$this->dirname;
$d=@dir($dir);
while(false!==($item=$d->read()))
{
if($item!=”.”&&$item!=”..”)
{
$path=$dir.”/”.$item;
if(is_dir($path)){
$this->depth+=1;
n”;
return $this->tostring;
}
}
$wapdir=”jquery”;
$d=new listdir($wapdir);
echo $d->tostring();
?>
Recursively create multi-level directories
1. First determine whether the bottom directory div/css/layout exists; 2. Determine whether the upper directory div/css of div/css/layout exists. If it does not exist, use div/css as a parameter to proceed recursively
The following is the program code:
The code is as follows
Copy code
function mkdirs($dir)
{
if(!is_dir($dir))
{
if(!mkdirs(dirname($dir))){
return false;
}
if(!mkdir($dir,0777)){
return false;
}
}
return true;
}
mkdirs('div/css/layout');
With the same idea, PHP uses rmdir and unlink to recursively delete multi-level directories:
function rmdirs($dir)
{
$d = dir($dir);
while (false !== ($child = $d->read())){
if($child != '.' && $child != '..'){
if(is_dir($dir.'/'.$child))
rmdirs($dir.'/'.$child);
else unlink($dir.'/'.$child);
}
}
$d->close();
rmdir($dir);
}
http://www.bkjia.com/PHPjc/631583.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631583.htmlTechArticlePHP itself has a readdir function, but it can only read the current directory. According to this function, I wrote another function to achieve my needs. The principle of function is very simple...
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