note 109141 modified in function.ftp-size by danbrown

From: Date: Tue, 26 Jun 2012 13:53:54 +0000
Subject: note 109141 modified in function.ftp-size by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-188828@lists.php.net to get a copy of this message
To overcome the 2 GB limitation, the ftp_raw solution below is probably the nicest. You can also perform this command using regular FTP commands:&#13;&#10;&#13;&#10;&#60;?php&#13;&#10;$response = ftp_raw($ftpConnection, &#34;SIZE $filename&#34;);&#13;&#10;$filesize = floatval(str_replace(&#39;213 &#39;, &#39;&#39;, $response[0]));&#13;&#10;?&#62;&#13;&#10;&#13;&#10;[However, this] 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:&#13;&#10;&#13;&#10;array(1) {&#13;&#10; [0]=&#62;&#13;&#10; string(18) &#34;550 File not found&#34;&#13;&#10;}&#13;&#10;&#13;&#10;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.&#13;&#10;&#13;&#10;&#60;?php&#13;&#10;$response = ftp_raw($ftp, &#34;SIZE $filename&#34;);&#13;&#10;$responseCode = substr($response[0], 0, 3);&#13;&#10;$responseMessage = substr($response[0], 4);&#13;&#10;?&#62;&#13;&#10;&#13;&#10;Or with a regular expression:&#13;&#10;&#13;&#10;&#60;?php&#13;&#10;$response = ftp_raw($ftp, &#34;SIZE $filename&#34;);&#13;&#10;if (preg_match(&#34;/^(\\d{3}) (.*)$/&#34;, $response[0], $matches) == 0)&#13;&#10; throw new Exception(&#34;Unable to parse FTP reply: &#34;.$response[0]);&#13;&#10;list($response, $responseCode, $responseMessage) = $matches;&#13;&#10;?&#62;&#13;&#10;&#13;&#10;You could then decide to assume that response code &#39;550&#39; means that it&#39;s a directory. I guess that&#39;s just as &#39;dangerous&#39; as assuming that ftp_size -1 means that it&#39;s a directory. --was-- 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. http://php.net/manual/en/function.ftp-size.php

« previous php.notes (#188828) next »