From 47f44b496bd855db280c0981835036e6e4990da2 Mon Sep 17 00:00:00 2001 From: David Grudl Date: Mon, 11 Jan 2010 21:45:27 +0100 Subject: [PATCH] added deleteRecursive() --- ftp.class.php | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/ftp.class.php b/ftp.class.php index 6d7f422..212230c 100644 --- a/ftp.class.php +++ b/ftp.class.php @@ -46,6 +46,27 @@ class Ftp + /** + * @param string URL ftp://... + */ + public function __construct($url = NULL) + { + if (!extension_loaded('ftp')) { + throw new /*\*/Exception("PHP extension FTP is not loaded."); + } + if ($url) { + $parts = parse_url($url); + $this->connect($parts['host'], empty($parts['port']) ? NULL : (int) $parts['port']); + $this->login($parts['user'], $parts['pass']); + $this->pasv(TRUE); + if (isset($parts['path'])) { + $this->chdir($parts['path']); + } + } + } + + + /** * Magic method (do not call directly). * @param string method name @@ -184,10 +205,29 @@ public function mkDirRecursive($dir) } } + + + /** + * Recursive deletes path. + * @param string + * @return void + */ + public function deleteRecursive($path) + { + if (!$this->tryDelete($path)) { + foreach ((array) $this->nlist($path) as $file) { + if ($file !== '.' && $file !== '..') { + $this->deleteRecursive(strpos($file, '/') === FALSE ? "$path/$file" : $file); + } + } + $this->rmdir($path); + } + } + } class FtpException extends Exception { -} \ No newline at end of file +}