Home > Backend Development > PHP Problem > How to traverse and delete directories in php

How to traverse and delete directories in php

藏色散人
Release: 2023-03-08 14:06:01
Original
2192 people have browsed it

How to traverse and delete a directory in php: first create a PHP sample file; then use the "function del_dir($dir) {...}" method to traverse and delete the directory and all files in the directory.

How to traverse and delete directories in php

#The operating environment of this article: Windows7 system, PHP7.1, Dell G3 computer.

PHP traverses and deletes the directory and all files in the directory

Code

function del_dir($dir) {
    if (!is_dir($dir)) {
        return false;
    }
    $handle = opendir($dir);
    while (($file = readdir($handle)) !== false) {
        if ($file != "." && $file != "..") {
            is_dir("$dir/$file") ? del_dir("$dir/$file") : @unlink("$dir/$file");
        }
    }
    if (readdir($handle) == false) {
        closedir($handle);
        @rmdir($dir);
    }
}
Copy after login

[Recommended: "PHP Video Tutorial"]

The above is the detailed content of How to traverse and delete directories 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