为了克服 2 GB 的限制,下面的 ftp_raw 解决方案可能是最好的。您也可以使用常规 FTP 命令执行此命令
<?php
$response = ftp_raw($ftpConnection, "SIZE $filename");
$filesize = floatval(str_replace('213 ', '', $response[0]));
?>
[但是,这]不足以用于目录。根据 RFC 3659 (http://tools.ietf.org/html/rfc3659#section-4.2),如果命令在文件以外的其他对象上发出,或者发生其他错误,服务器应该返回错误 550(找不到文件)。例如,Filezilla 在对目录使用 ftp_raw 命令时确实返回此字符串
array(1) {
[0]=>
string(18) "550 File not found"
}
RFC 959 (http://tools.ietf.org/html/rfc959) 规定返回的字符串始终由正好 3 位数字、1 个空格以及某些文本组成。(允许多行文本,但我忽略了这一点。)因此,最好使用 substr 甚至正则表达式来分割字符串。
<?php
$response = ftp_raw($ftp, "SIZE $filename");
$responseCode = substr($response[0], 0, 3);
$responseMessage = substr($response[0], 4);
?>
或者使用正则表达式
<?php
$response = ftp_raw($ftp, "SIZE $filename");
if (preg_match("/^(\\d{3}) (.*)$/", $response[0], $matches) == 0)
throw new Exception("无法解析 FTP 回复:".$response[0]);
list($response, $responseCode, $responseMessage) = $matches;
?>
然后您可以决定假设响应代码“550”表示它是目录。我想这和假设 ftp_size -1 表示它是目录一样“危险”。