How to delete folder contents in php

藏色散人
Release: 2023-03-03 15:42:01
Original
2898 people have browsed it

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;
    }
}
Copy after login

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!

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