要克服 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(找不到檔案)。例如,在目錄上使用 ftp_raw 指令時,Filezilla 確實會傳回這個字串
array(1) {
[0]=>
字串(18) "550 檔案未找到"
}
RFC 959 (http://tools.ietf.org/html/rfc959) 規定回傳的字串永遠是由三個數字,接著一個空格,然後是一段文字所組成。(允許多行文字,但我現在先忽略這個情況。)所以最好使用 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 表示它是一個目錄一樣「危險」。