note 71086 added to function.ftp-rmdir

From: Date: Sat, 11 Nov 2006 10:47:15 +0000
Subject: note 71086 added to function.ftp-rmdir
Groups: php.notes 
Request: Send a blank email to php-notes+get-119604@lists.php.net to get a copy of this message
The problem with the previous posts about recursive deleting of directories is that you might need to set error_reporting(0), but this doesn't work always. When you've got your own error_handler set, you'll notice that an error is thrown even when setting error_reporting(0). The following script generates various errors: <?php function ftp_rmdirr($path, $handle) { if (!(@ftp_rmdir($handle, $path) || @ftp_delete($handle, $path))) { $list = ftp_nlist($handle, $path); if (!empty($list)) foreach($list as $value) ftp_rmdirr($value, $handle); } @ftp_rmdir($handle, $path); } ?> Let me explain: with the @-sign you can suppres the error generated if the command failed, but somehow it still outputs the error. So the easiest solution for this is to check if the path is to a file or a directory, which you can check with ftp_nlist or ftp_rawlist. ftp_nlist(filename) returns the filename, ftp_nlist(dirname) returns a list with the files in that directory, or returns an empty array if the directory is empty. If ftp_nlist is not working, you can use ftp_rawlist() and parse the results. With ftp_rawlist(resource, path) you can get a detailed list of the files in a specific directory. At the end of every element of the array that ftp_rawlist(resource, path) returns, you'll find the filename or the dirname. This file- or dirname you can precede with the path you've provided for ftp_rawlist(), which gives you the ability to check if a path to a file or directory is really a file or a directory. This is what I came up with: <? /** * 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) { $list = ftp_nlist ($resource, $path); if ( empty($nlist) ){ $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."."){ DeleteDirRecursive ($item); } } ftp_rmdir ($resource, $path); } else { ftp_delete ($resource, $path); } } /** * 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: 82.146.33.222 Probable Submitter: 84.196.198.109 ---- Manual Page -- http://www.php.net/manual/en/function.ftp-rmdir.php Edit -- https://master.php.net/note/edit/71086 Del: integrated -- https://master.php.net/note/delete/71086/integrated Del: useless -- https://master.php.net/note/delete/71086/useless Del: bad code -- https://master.php.net/note/delete/71086/bad+code Del: spam -- https://master.php.net/note/delete/71086/spam Del: non-english -- https://master.php.net/note/delete/71086/non-english Del: in docs -- https://master.php.net/note/delete/71086/in+docs Del: other reasons-- https://master.php.net/note/delete/71086 Reject -- https://master.php.net/note/reject/71086 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#119604) next »