PHP delete all files and folders under the specified directory | delete the specified file

    /**
     * Delete the directory and all files in the directory or delete the specified file
     * @param str $path the path of the directory to be deleted
     * @param int $delDir Whether to delete the directory, 1 or true to delete the directory, 0 or false to delete only the file retention directory (including subdirectories)
     * @return bool returns the deleted status
     */
    function delDirAndFile($path, $delDir = FALSE) {
        $handle = opendir($path);
        if ($handle) {
            while (false !== ( $item = readdir($handle) )) {
                if ($item != "." && $item != "..")
                    is_dir("$path/$item") ? delDirAndFile("$path/$item", $delDir) : unlink("$path/$item");
            }
            closedir($handle);
            if ($delDir)
                return rmdir($path);
        }else {
            if (file_exists($path)) {
                return unlink($path);
            } else {
                return FALSE;
            }
        }
    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325851404&siteId=291194637