PHP does not provide the function of querying directory size, copying directories, deleting directories with files, we need to write our own
This function simply passes a URL and can recursively query the file size in the directory to count the size of the directory
1
Php2 functionDirsize ($dir){3 $dirsize= 0;4 $url=Opendir($dir);5 while($filename=Readdir($url)){6 $file=$dir.' /'.$filename;7 if(! ($filename=='.' ||$filename=='..')){8 if(Is_dir($file)){9 $dirsize+ = Dirsize ($file);Ten}Else{ One $dirsize+=filesize($file); A } - } - } the Closedir($url); - return $dirsize; -}
This function needs to pass two URLs, namely the old directory and the new directory, to copy the directory and the file
1
Php2 functionCopydir ($DIRSRC,$dirTo){3 if(Is_file($dirTo))4 {5 Echo' Destination is not a directory cannot be created! ';6 return;7 }8 if(!file_exists($dirTo))9 {Ten mkdir($dirTo); One } A $dir _handle= @Opendir($DIRSRC); - if($dir _handle) - { the while($filename=Readdir($dir _handle)) - { - if($filename! = "." &&$filename!="..") - { + $subSrcFile=$DIRSRC. "\\".$filename; - $subToFile=$dirTo. "\\".$filename; + A if(Is_dir($subSrcFile)) at { -Copydir ($subSrcFile,$subToFile); - } - if(Is_file($subSrcFile)) - { - Copy($subSrcFile,$subToFile); in } - } to } + Closedir($dir _handle); - } the}
This function simply passes a URL, can delete the directory and subdirectories of the file recursively, thus deleting the directory
1
Php2 functionRmdirall ($url){3 //detects whether the directory to open exists4 if(!$dir=@Opendir($url)){5 return false;6 }7 while(false!==($file=Readdir($dir))){8 //exclude current directory from parent directory9 if($file!=="." &&$file!==".."){Ten $file=$url.' /'.$file; One //recursively delete a directory A if(Is_dir($file)){ -Rmdirall ($file); -}Else{ the if(@unlink($file)){ - Continue; -}Else{ - return false; + } - } + } A if(@rmdir($url)){ at return true; -}Else{ - return false; - } - } -}
The above describes the directory of custom functions, including aspects of the content, I hope to be interested in PHP tutorial friends helpful.