Home  >  Article  >  Backend Development  >  How to delete folder contents in php

How to delete folder contents in php

藏色散人
藏色散人Original
2020-07-31 10:29:242903browse

php method to delete the contents of a folder: first create a PHP sample file; then define a deldir method; then open the file directory through the opendir function; finally delete the files and empty folders in the directory recursively. Can.

How to delete folder contents in php

Recommended: "PHP Video Tutorial"

phpDelete the folder and its contents

The code is as follows:

<?php

function deldir($dir) {
    //先删除目录下的文件:
    $dh=opendir($dir);
    while ($file=readdir($dh)) {

        if($file!="." && $file!="..") {

            $fullpath=$dir."/".$file;

            if(!is_dir($fullpath)) {
                unlink($fullpath);
            } else {
                deldir($fullpath);// 递归
            }
        }
    }
    closedir($dh);

    // 删除空文件夹:递归
    if(rmdir($dir)) {
        return true;
    } else {
        return false;
    }
}

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

Statement:
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