如果您想監控下載進度,可以使用 filesize() 函式。
但請注意:該函式的結果會被快取,所以您總是會得到 0 位元組。在呼叫 filesize() 以確定已下載檔案的實際大小之前,請先呼叫 clearstatcache()。
這可能會影響效能,但如果您想提供資訊,這是不可避免的。
以上範例延伸
<?php
// 取得遠端檔案的大小
$fs = ftp_size($my_connection, "test");
// 開始下載
$ret = ftp_nb_get($my_connection, "test", "README", FTP_BINARY);
while ($ret == FTP_MOREDATA) {
clearstatcache(); // <- 這很重要
$dld = filesize($locfile);
if ( $dld > 0 ){
// 計算百分比
$i = ($dld/$fs)*100;
printf("\r\t%d%% 已下載", $i);
}
// 繼續下載...
$ret = ftp_nb_continue ($my_connection);
}
if ($ret != FTP_FINISHED) {
echo "下載檔案時發生錯誤...";
exit(1);
}
?>
Philip