Page 1 of 1

reading text-file

Posted: Tue Sep 01, 2009 2:27 am
by jorgeng
I have a text-file which i reads with fread, but the file is sometimes empy, then i got error:

Warning: fread() [function.fread]: Length parameter must be greater than 0 in C:\Program Files\xampp\htdocs\R.php on line 91

How do i solve this?

Here is the code:

Code: Select all

 
// make sure the file is successfully opened before doing anything else
if ($fp2 = fopen($file, 'r')) {
   $content = '';
   // keep reading until there's nothing left 
   while ($line2 = fread($fp2, 1024)) {
        $content2 .= $line2;
        $data2 = strstr($content2,"kurs ");
        $pris = substr($data2, 5, 6);
        echo "$pris <br />";        
   }
   // do something with the content here   
} else {
   // an error occured when trying to open the specified file 
   // do something here ... ?
   echo "Kunde ej öppna html-filen"; 
}
 
I need some error-handling when the infile is empty, can someone help me?
:(

Re: reading text-file

Posted: Tue Sep 01, 2009 3:11 am
by papa

Code: Select all

 
if(filesize($filename) >0) {
}
else echo "Filen är tom.";