PHP View folder size, copy folder, delete folder

Source: Internet
Author: User

PHP provides functions such as filesize, copy, unlink, and so on, but does not provide functions such as dirsize, Copydir, Rmdirs and Other folder operations (RmDir can only delete empty directories). So you can only write these functions manually, and the main trick is to decompose the problem by layer by recursion until it breaks down into the least-boy problem that can be solved directly.

==================== View folder size =====================

Because the folder is no size, the usual size of the folder is the size of all the files in the folder . So you just need to find the folder layer by level, count the file size of each layer, and finally return the results from the deepest folder to merge.

/** * Folder size * @param $path * @return int*/functionDirsize ($path){    $size= 0; $handle=Opendir($path);  while(($item=Readdir($handle)) !==false) {        if($item= = '. ' | |$item= = '.. ')Continue; $_path=$path. ‘/‘ .$item; if(Is_file($_path))$size+=filesize($_path); if(Is_dir($_path))$size+ = Dirsize ($_path); }    Closedir($handle); return $size;}

===================== Copy folder ======================

Files can be copied, folders cannot be copied (but can be created), if you want to achieve the effect of the Copy folder, you need to generate a new folder with the same directory structure as the original folder, and then copy the files to the new folder according to the original directory structure. According to the idea of recursion, you do not have to build the complete directory structure at the beginning, just traverse the current directory, discover that the folder does not exist and create it, and then copy the file to the corresponding folder.

/** * Copy folder * @param $source * @param $dest*/functionCopydir ($source,$dest){    if(!file_exists($dest))mkdir($dest); $handle=Opendir($source);  while(($item=Readdir($handle)) !==false) {        if($item= = '. ' | |$item= = '.. ')Continue; $_source=$source. ‘/‘ .$item; $_dest=$dest. ‘/‘ .$item; if(Is_file($_source))Copy($_source,$_dest); if(Is_dir($_source)) Copydir ($_source,$_dest); }    Closedir($handle);}

===================== Delete folder ======================

RmDir can only delete empty folders, and unlink delete files. So the idea of deleting the folder has, first through unlink delete all files under the directory, and then through rmdir Delete the remaining empty folder. The logic of using recursive processing is to find all the files and folders in the current directory, delete all the files in it, and then traverse the next level directory to delete all the files ... Until the last level of the directory, the entire folder is no longer a file (but the folder is still in), at this point, the recursive start to return, each up and down to remove all the empty directory, when the top level of the return to the end of the recursion, and all the files and directories are deleted.

/** * Delete folder * @param $path * @return bool*/functionRmdirs ($path){    $handle=Opendir($path);  while(($item=Readdir($handle)) !==false) {        if($item= = '. ' | |$item= = '.. ')Continue; $_path=$path. ‘/‘ .$item; if(Is_file($_path))unlink($_path); if(Is_dir($_path)) Rmdirs ($_path); }    Closedir($handle); return rmdir($path);}

===================== Cut folder ======================

Rename is a special case in the PHP Filesystem function, which can either rename the file or rename the folder. If you pass the renamed file to a different path, it becomes a clipping function, which is a typical example of small and beautiful file functions.

/**/rename($oldname,$newname,$context);

PHP View folder size, copy folder, delete folder

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.