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

Recursively delete directories and files in rrmdir php

Dec 28, 2016 pm 03:02 PM

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

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

For more articles related to recursively deleting directories and files in directories in rrmdir php, please pay attention to the PHP Chinese website!

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

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1659
14
PHP Tutorial
1257
29
C# Tutorial
1231
24