Home  >  Article  >  php教程  >  Recursively delete directories and files in rrmdir php

Recursively delete directories and files in rrmdir php

高洛峰
高洛峰Original
2016-12-28 15:02:311312browse

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().

 
[/code] 
A patch to previous script to make sure rights for deletion is set:

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