PHP Conference Japan 2024

SplFileInfo::getSize

(PHP 5 >= 5.1.2, PHP 7, PHP 8)

SplFileInfo::getSize取得檔案大小

描述

public SplFileInfo::getSize(): int|false

傳回所參考檔案的檔案大小(以位元組為單位)。

參數

此函式沒有參數。

傳回值

成功時傳回檔案大小(以位元組為單位),失敗時傳回 false

錯誤/例外

如果檔案不存在或發生錯誤,將會拋出 RuntimeException

範例

範例 #1 SplFileInfo::getSize() 範例

<?php
$info
= new SplFileInfo('example.jpg');
echo
$fileinfo->getFilename() . " " . $fileinfo->getSize();
?>

以上範例會輸出類似下列的結果

example.jpg 15385

參見

新增註解

使用者貢獻的註解 4 則註解

24
random-citizen at example dot org
6 年前
如果您正在使用 Symfony 的 UploadedFile,
請注意,如果您在呼叫 @move 之後
呼叫此方法,您很可能會得到
一些難以追蹤的錯誤,上面寫著

`stat failed`

如果您仔細想想,這確實有道理,
該檔案已被 Symfony 移動,但 getSize 位於 SplFileInfo 中,
而且 SplFileInfo 不知道該檔案已被移動。

奇怪的是,我的工作 Mac 上沒有出現該錯誤:|
1
匿名
8 年前
請查看 https://php.dev.org.tw/manual/en/function.filesize.php#115792,取得在 32 位元系統上處理 >2GB 檔案的快速可靠檔案大小版本。
2
Pawel B.
1 年前
當 getSize 在 fwrtite 之後傳回 0 時,您必須使用 clearstatcache

$tmpFile = new \SplFileObject('/tmp/file.txt');
$fp = $tmpFile->openFile('w');
$fp->fwrite('123');
$fp->fflush();
echo $fp->getSize(); // 傳回 0
clearstatcache();
echo $fp->getSize(); // 傳回 3

https://bugs.php.net/bug.php?id=72182
-1
franssen dot roland at gmail dot com
13 年前
如果檔案不存在,似乎會傳回 FALSE... (PHP 5.3.4)
To Top