PHP 8.1.28 Released!


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

SplFileInfo::getSizeLit la taille d'un fichier


public SplFileInfo::getSize(): int|false

Retourne la taille d'un fichier, en octets.

Liste de paramètres

Cette fonction ne contient aucun paramètre.

Valeurs de retour

La taille du fichier, en octets en cas de succès ou false si une erreur survient.

Erreurs / Exceptions

Une exception RuntimeException sera émise si le fichier n'existe pas, ou si une erreur survient.


Exemple #1 Exemple avec SplFileInfo::getSize()

= new SplFileInfo('example.jpg');
$fileinfo->getFilename() . " " . $fileinfo->getSize();

Résultat de l'exemple ci-dessus est similaire à :

example.jpg 15385

Voir aussi

add a note

User Contributed Notes 5 notes

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 :|
8 years ago
Check for fast and reliable version of filesize for files >2gb on 32 bit systems.
Pawel B.
11 months ago
When getSize return 0, after fwrtite, You must use clearstatcache:

$tmpFile = new \SplFileObject('/tmp/file.txt');
$fp = $tmpFile->openFile('w');
echo $fp->getSize(); //Return 0
echo $fp->getSize(); //Return 3
franssen dot roland at gmail dot com
12 years ago
Seems to return FALSE if file does not exists... (PHP 5.3.4)
contact at socialdevelop dot biz
8 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