note 81616 added to function.ftp-rmdir

From: Date: Thu, 06 Mar 2008 12:16:07 +0000
Subject: note 81616 added to function.ftp-rmdir
Groups: php.notes 
Request: Send a blank email to php-notes+get-136786@lists.php.net to get a copy of this message
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; } ---- Server IP: 194.153.94.10 Probable Submitter: 83.228.157.182 ---- Manual Page -- http://www.php.net/manual/en/function.ftp-rmdir.php Edit -- https://master.php.net/note/edit/81616 Del: integrated -- https://master.php.net/note/delete/81616/integrated Del: useless -- https://master.php.net/note/delete/81616/useless Del: bad code -- https://master.php.net/note/delete/81616/bad+code Del: spam -- https://master.php.net/note/delete/81616/spam Del: non-english -- https://master.php.net/note/delete/81616/non-english Del: in docs -- https://master.php.net/note/delete/81616/in+docs Del: other reasons-- https://master.php.net/note/delete/81616 Reject -- https://master.php.net/note/reject/81616 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#136786) next »