How can I get rid of filesize () warning in PHP?

Besides disabling warnings .. what can I do in my code to get rid of this error:

warning: filesize() [function.filesize] stat failed for .... on line 152 

Code:

 $store_filesize = filesize($file->filepath); 
+4
source share
5 answers

I'm not sure if this is what you want (for example, I donโ€™t understand if you want to figure out why this is happening and get around it), but if you just want to suppress errors, you can usually use @in in front of the function name.

eg.

 $store_filesize = @filesize($file->filepath); 
+6
source

Do not call filesize if the file does not exist:

 $store_filesize = is_file($file->filepath) ? filesize($file->filepath) : 0; 

Docs for is_file : http://php.net/manual/en/function.is-file.php

:)

+3
source
 $store_filesize = @filesize($file->filepath); if ($store_filesize === FALSE) { throw new \Exception('filesize failed'); } 
+2
source

Remember to check for null :

 if($file->filepath != null){ $store_filesize = filesize($file->filepath); } 
+1
source

Check if a file exists, is a file, and is readable

 if (is_readable($file->filepath)) { $store_filesize = filesize($file->filepath); } 
+1
source

All Articles