note 109141 added to function.ftp-size

From: Date: Fri, 22 Jun 2012 19:25:53 +0000
Subject: note 109141 added to function.ftp-size
Groups: php.notes 
Request: Send a blank email to php-notes+get-188759@lists.php.net to get a copy of this message
The code in my previous comment is insufficient for use on directories. As per RFC 3659 (http://tools.ietf.org/html/rfc3659#section-4.2), servers should return error 550 (File not found) if the command is issued on something else than a file, or if some other error occurred. For example, Filezilla indeed returns this string when using the ftp_raw command on a directory: array(1) { [0]=> string(18) "550 File not found" } RFC 959 (http://tools.ietf.org/html/rfc959) dictates that the returned string always consists of exactly 3 digits, followed by 1 space, followed by some text. (Multi-line text is allowed, but I am ignoring that.) So it is probably better to split the string with substr, or even a regular expression. <?php $response = ftp_raw($ftp, "SIZE $filename"); $responseCode = substr($response[0], 0, 3); $responseMessage = substr($response[0], 4); ?> Or with a regular expression: <?php $response = ftp_raw($ftp, "SIZE $filename"); if (preg_match("/^(\d{3}) (.*)$/", $response[0], $matches) == 0) throw new Exception("Unable to parse FTP reply: ".$response[0]); list($response, $responseCode, $responseMessage) = $matches; ?> You could then decide to assume that response code '550' means that it's a directory. I guess that's just as 'dangerous' as assuming that ftp_size -1 means that it's a directory. ---- Server IP: 91.199.167.223 Probable Submitter: 212.142.33.197 ---- Manual Page -- http://www.php.net/manual/en/function.ftp-size.php Edit -- https://master.php.net/note/edit/109141 Del: integrated -- https://master.php.net/note/delete/109141/integrated Del: useless -- https://master.php.net/note/delete/109141/useless Del: bad code -- https://master.php.net/note/delete/109141/bad+code Del: spam -- https://master.php.net/note/delete/109141/spam Del: non-english -- https://master.php.net/note/delete/109141/non-english Del: in docs -- https://master.php.net/note/delete/109141/in+docs Del: other reasons-- https://master.php.net/note/delete/109141 Reject -- https://master.php.net/note/reject/109141 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#188759) next »