SplFileInfo::getSize

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

SplFileInfo::getSizeObtém o tamanho do arquivo

Descrição

public SplFileInfo::getSize(): int|false

Retorna o tamanho do arquivo em bytes para o arquivo referenciado.

Parâmetros

Esta função não possui parâmetros.

Valor Retornado

O tamanho do arquivo em bytes em caso de sucesso, ou false em caso de falha.

Erros/Exceções

Um RuntimeException será lançado se o arquivo não existir ou ocorrer um erro.

Exemplos

Exemplo #1 Exemplo de SplFileInfo::getSize()

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

O exemplo acima produzirá algo semelhante a:

example.jpg 15385

Veja Também

add a note

User Contributed Notes 5 notes

up
22
random-citizen at example dot org
5 years ago
If you're using Symfony's UploadedFile,
please be aware that if you call this method
_after_ you call @move, you will most likely get
some obscenely untraceable error, that says:

`stat failed`

Which if you really think about it, it does makes sense,
the file has been moved by Symfony, but getSize is in SplFileInfo,
and SplFileInfo doesn't know that the file has been moved.

Weirdly enough, that error doesn't come on my work mac :|
up
1
Anonymous
7 years ago
Check http://php.net/manual/en/function.filesize.php#115792 for fast and reliable version of filesize for files >2gb on 32 bit systems.
up
2
Pawel B.
9 months ago
When getSize return 0, after fwrtite, You must use clearstatcache:

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

https://bugs.php.net/bug.php?id=72182
up
-1
franssen dot roland at gmail dot com
12 years ago
Seems to return FALSE if file does not exists... (PHP 5.3.4)
up
-9
contact at socialdevelop dot biz
7 years ago
if use it as SplFileInfo::getSize - it return false if file not found
if use it as SplFileObject::getSize - it generate RuntimeException: SplFileObject::__construct(my-file): failed to open stream: No such file or directory
To Top