search
Homephp教程PHP开发Recursively delete directories and files in rrmdir php

function rrmdir($dir) { 
if (is_dir($dir)) { 
$objects = scandir($dir); 
foreach ($objects as $object) { 
if ($object != “.” && $object != “..”) { 
if (filetype($dir.”/”.$object) == “dir”) rrmdir($dir.”/”.$object); else unlink($dir.”/”.$object); 
} 
} 
reset($objects); 
} 
}

rmdir
(PHP 4, PHP 5)
rmdir — Delete directory
Report a bug Description
bool rmdir (string $dirname)
Try to delete the directory specified by dirname. The directory must be empty and must have appropriate permissions. Returns TRUE on success, or FALSE on failure.
Note: Since PHP 5.0.0 rmdir() can also be used with certain URL wrapping protocols. See the list of Supported Protocols and Wrappers to see which URL wrapping protocols rmdir() supports.
Note: Support for context (Context) was added in PHP 5.0.0. See the Stream function for a description of context.
Note: When safe mode is enabled, PHP will check whether the directory being scripted has the same UID (owner) as the script being executed when executing the script.
See mkdir() and unlink().

<?php 
function rrmdir($dir) { 
if (is_dir($dir)) { 
$objects = scandir($dir); 
foreach ($objects as $object) { 
if ($object != "." && $object != "..") { 
if (filetype($dir."/".$object) == "dir") rrmdir($dir."/".$object); else unlink($dir."/".$object); 
} 
} 
reset($objects); 
rmdir($dir); 
} 
} 
?>
<?php 
function deleteAll($directory, $empty = false) { 
if(substr($directory,-1) == "/") { 
$directory = substr($directory,0,-1); 
} 
if(!file_exists($directory) || !is_dir($directory)) { 
return false; 
} elseif(!is_readable($directory)) { 
return false; 
} else { 
$directoryHandle = opendir($directory); 
while ($contents = readdir($directoryHandle)) { 
if($contents != &#39;.&#39; && $contents != &#39;..&#39;) { 
$path = $directory . "/" . $contents; 
if(is_dir($path)) { 
deleteAll($path); 
} else { 
unlink($path); 
} 
} 
} 
closedir($directoryHandle); 
if($empty == false) { 
if(!rmdir($directory)) { 
return false; 
} 
} 
return true; 
} 
} 
?> 
[/code] 
A patch to previous script to make sure rights for deletion is set:
<?php 
//Delete folder function 
function deleteDirectory($dir) { 
if (!file_exists($dir)) return true; 
if (!is_dir($dir) || is_link($dir)) return unlink($dir); 
foreach (scandir($dir) as $item) { 
if ($item == &#39;.&#39; || $item == &#39;..&#39;) continue; 
if (!deleteDirectory($dir . "/" . $item)) { 
chmod($dir . "/" . $item, 0777); 
if (!deleteDirectory($dir . "/" . $item)) return false; 
}; 
} 
return rmdir($dir); 
} 
?>

For more articles related to recursively deleting directories and files in directories in rrmdir php, please pay attention to 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft