I’m using script like this:
$path = dirname(__FILE__)."/folder/file.ext"
if(is_readable($path)){
$file = fread(fopen($path,"r"), filesize($path));
}else{
//do something else if file can't be opened
}
And surprisingly it generates the following error:
Warning: fread() [function.fread]: Length parameter must be greater than 0
in /nfs/c10/h02/mnt/161920/domains/example.com/html/my-folder/file.php on line 16
I’m wondering why this happens. It happens only on some servers and on others it’s fine. What are requirements to use fread successfully? File exists in that location.
As everyone has said, filesize is probably 0.
Either use
file_get_contentsas suggested, or if you absolutely want/need to usefread(), you could get around it with the following on php 5.3+:Notice the
?: 1? It’s called a ternary operator. That says, use filesize if it evaluates to true (i.e. it’s bigger than 0) or use 1.Definitely not best practice but fun to point out.