Home > Backend Development > PHP Problem > How to delete a folder and all its contents in php

How to delete a folder and all its contents in php

PHPz
Release: 2023-03-27 19:28:22
Original
2496 people have browsed it

Deleting files and folders in PHP is easy, but if you want to delete an entire folder and all its contents, you will need to spend some time and effort writing code.

PHP provides some functions to handle file and folder operations, the most commonly used of which are the "rmdir()" and "unlink()" functions. The "rmdir()" function is used to delete an empty folder. If the folder contains files or other folders, an error will be thrown. The "unlink()" function is used to delete a file and throws an error if the file does not exist.

Now, we need to delete a non-empty folder and delete all its contents. There are two ways to achieve this goal.

Method 1: Recursively delete a folder and all its contents

The idea of ​​recursively deleting a folder is to first traverse all subfolders and files in the folder, The function is then called recursively until all files and folders are deleted. Here is an example:

function delete_directory($dir) {
    if (!file_exists($dir)) {
        return true;
    }
    if (!is_dir($dir)) {
        return unlink($dir);
    }
    foreach (scandir($dir) as $item) {
        if ($item == '.' || $item == '..') {
            continue;
        }
        if (!delete_directory($dir . DIRECTORY_SEPARATOR . $item)) {
            return false;
        }
    }
    return rmdir($dir);
}
Copy after login

Explain the code of this function:

  • Returns true if the folder does not exist.
  • If the target is a file rather than a folder, use the unlink() function to delete the file.
  • Traverse all files and folders in the target folder, and return false if the file or folder fails to be deleted during the recursive process.
  • If all files and folders have been deleted, delete the target folder.

Note that this method deletes all content, including empty folders under the folder, even if you don’t want to delete empty folders.

Method 2: Use the standard library file system class to delete the folder and its contents

Another method is to use the "RecursiveDirectoryIterator" and "RecursiveIteratorIterator" in the PHP standard library " classes that make it easy to delete a folder and all its contents.

function delete_directory($dir) {
    $it = new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS);
    $files = new RecursiveIteratorIterator($it,
                 RecursiveIteratorIterator::CHILD_FIRST);
    foreach($files as $file) {
        if ($file->isDir()){
            rmdir($file->getRealPath());
        } else {
            unlink($file->getRealPath());
        }
    }
    rmdir($dir);
}
Copy after login

Similarly, this function uses recursion, but it uses the RecursiveDirectoryIterator class and RecursiveIteratorIterator class in the PHP standard library to recursively traverse all files and folders in the folder. The first parameter is the folder path, and the second parameter is an option, which means skipping the folders "." and "..". If a folder is encountered during the traversal, the rmdir() function is used to delete the folder. If a file is encountered, the unlink() function is used to delete the file.

Summary

There are two ways to delete a folder and all its contents in PHP: recursive deletion and using the standard library file system classes. Which method you choose depends on your needs and personal preferences. One thing to note when using recursive functions is that you need to make sure that all folders have been deleted, otherwise it may cause unnecessary problems. When using the standard library filesystem classes, you need to use the appropriate options to skip the "." and ".." folders, and you need to pass the folder path to the function.

The above is the detailed content of How to delete a folder and all its contents in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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