Home > Backend Development > PHP Tutorial > Examples of how to use recursion to copy, delete, and view the size of folders in PHP

Examples of how to use recursion to copy, delete, and view the size of folders in PHP

黄舟
Release: 2023-03-14 21:34:01
Original
1037 people have browsed it

This article mainly introduces the PHP recursive implementation of folder copying, deletion, and viewing size operations. It analyzes the PHP recursive algorithm combined with PHP common folder operation functions to implement folder copying, deletion, viewing, and other related implementations in the form of examples. For tips, friends who need them can refer to

. The example in this article describes the recursive copying, deleting, and viewing size operations of folders in PHP. Share it with everyone for your reference, as follows:

Although PHP provides functions for file operations such as filesize, copy, and unlink, it does not provide functions for folder operations such as dirsize, copydir, and rmdirs (rmdir can only delete empty directories). So these functions can only be written manually. The main trick is to decompose the problem layer by layer through recursion until it is decomposed into the smallest sub-problem that can be solved directly.

Check the folder size

Since folders have no size, the folder size usually refers to the folder size to be precise. The total size of all files in . So you only need to search the folders layer by layer, count the file sizes contained in each layer, and finally return the results from the deepest folder to merge.


/**
 * 文件夹大小
 * @param $path
 * @return int
 */
function dirsize($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 after login

Copy folder

Files can be copied, but folders cannot be copied ( But it can be created), if you want to achieve the effect of copying a 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 recursive idea, there is no need to generate a complete directory structure at the beginning. You only need to traverse the current directory, find that the folder does not exist, create it, and then copy the file to the corresponding folder.


/**
 * 复制文件夹
 * @param $source
 * @param $dest
 */
function copydir($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);
}
Copy after login

Delete folders

rmdir can only delete empty folders, while unlink can delete files . So the idea of ​​deleting a folder came up. First delete all files in the directory through unlink, and then delete the remaining empty folders through rmdir. The logic of using recursive processing is to find all files and folders in the current directory, delete all files in them, and then traverse the next level directory to delete all files... until the last level directory, at which time there are no files in the entire folder ( But the folder is still there), at this time the recursion starts to return, and every time it returns to one level, all empty directories on this level are deleted. When it returns to the top level, the recursion ends, and all files and directories are also deleted. .


/**
 * 删除文件夹
 * @param $path
 * @return bool
 */
function rmdirs($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);
}
Copy after login

Cut folder

rename is a special case of the PHP Filesystem function, which is Files can be renamed, and folders can be renamed. If you pass in a different path for the renamed file, it becomes the cut function, which is a small but beautiful example of file functions.


/**
 * @param $oldname 必需。规定要重命名的文件或目录。
 * @param $newname 必需。规定文件或目录的新名称。
 * @param $context 可选。规定文件句柄的环境。
 */
rename($oldname,$newname,$context);
Copy after login

The above is the detailed content of Examples of how to use recursion to copy, delete, and view the size of folders in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template