note 81616 deleted from function.ftp-rmdir by danbrown

From: Date: Wed, 30 Jun 2010 12:04:13 +0000
Subject: note 81616 deleted from function.ftp-rmdir by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-170585@lists.php.net to get a copy of this message
Note Submitter: romain ---- I noticed 2 small mistakes in the code proposed by TSE_Webdesign... and added an error message. if ( empty($nlist) ) { ==> if ( empty($list) ) { and DeleteDirRecursive($item); ==> DeleteDirRecursive($resource, $item); So here is a correct version... /** * Delete the provided directory and all its contents from the FTP-server. * * @param string $path Path to the directory on the FTP-server relative to the current working directory */ function DeleteDirRecursive($resource, $path) { $result_message = ""; $list = ftp_nlist ($resource, $path); if ( empty($list) ) { $list = RawlistToNlist( ftp_rawlist($resource, $path), $path . ( substr($path, strlen($path) - 1, 1) == "/" ? "" : "/" ) ); } if ($list[0] != $path) { $path .= ( substr($path, strlen($path)-1, 1) == "/" ? "" : "/" ); foreach ($list as $item) { if ($item != $path.".." && $item != $path.".") { $result_message .= DeleteDirRecursive($resource, $item); } } if (ftp_rmdir ($resource, $path)) { $result_message .= "Successfully deleted $path <br />\n"; } else { $result_message .= "There was a problem while deleting $path <br />\n"; } } else { if (ftp_delete ($resource, $path)) { $result_message .= "Successfully deleted $path <br />\n"; } else { $result_message .= "There was a problem while deleting $path <br />\n"; } } return $result_message; } /** * Convert a result from ftp_rawlist() to a result of ftp_nlist() * * @param array $rawlist Result from ftp_rawlist(); * @param string $path Path to the directory on the FTP-server relative to the current working directory * @return array An array with the paths of the files in the directory */ function RawlistToNlist($rawlist, $path) { $array = array(); foreach ($rawlist as $item) { $filename = trim(substr($item, 55, strlen($item) - 55)); if ($filename != "." || $filename != "..") { $array[] = $path . $filename; } } return $array; }

« previous php.notes (#170585) next »