Shamoon
Shamoon

Reputation: 43569

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

Other than turning warnings off.. what can I do in my code to get rid of this error:

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

The code is:

$store_filesize = filesize($file->filepath);

Upvotes: 4

Views: 19115

Answers (5)

Chris Baker
Chris Baker

Reputation: 50602

Don't call filesize unless the file actually exists:

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

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

:)

Upvotes: 3

Ondřej Mirtes
Ondřej Mirtes

Reputation: 5661

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

Upvotes: 3

Maxim Krizhanovsky
Maxim Krizhanovsky

Reputation: 26719

Check if the file exists, is file and is readable

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

Upvotes: 1

Naftali
Naftali

Reputation: 146310

Remember to check for null:

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

Upvotes: 1

Iain Collins
Iain Collins

Reputation: 6884

I'm not sure if this is what you want (e.g. I'm not clear if you want to work out why it's happening and work around it) but if you are just looking to suppress errors you can usually use @ in front of the function name.

e.g.

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

Upvotes: 6

Related Questions