note 79844 deleted from function.ftp-rmdir by bjori
| From: | bjori@php.net | Date: | Sat, 15 Dec 2007 10:11:15 +0000 |
| Subject: | note 79844 deleted from function.ftp-rmdir by bjori | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-133535@lists.php.net to get a copy of this message | ||
Note Submitter: php DOT 20 DOT rinogo AT xoxy.net
Reason: useless
----
I got tired of trying other people's implementations of recursive FTP deletion, so I wrote my
own. It's full of helpful comments and straightforward PHP. Enjoy! (I'm running PHP 4.7
I believe)
Returns false on error, true on success.
/**
* Recursively delete a directory.
*
* @param string $resource FTP connection resource
* @param string $path Path to the directory on the FTP-server relative to the current working
directory
*/
function delete_dir_recursive($resource, $path) {
$list = ftp_nlist($resource, $path);
foreach($list as $item) {
//Skip . and ..
if($item=='.' || $item=='..')
continue;
//Delete files
elseif($item==$path) {
if(ftp_delete($resource, $path)===FALSE)
return FALSE;
}
//Recursively enter directories
else {
//If there were any errors in the recursive call execution, return immediately.
if(!delete_dir_recursive($resource, "$path/$item"))
return FALSE;
//If the directory is not yet empty, continue deleting entries.
if(sizeof(ftp_nlist($resource, $path))>2)
continue;
//Delete directories after we've deleted all files within them (*after* the recursive call)
if(ftp_rmdir($resource, $path)===FALSE)
return FALSE;
}
}
return TRUE;
}