reading text-file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jorgeng
Forum Commoner
Posts: 43
Joined: Mon Aug 04, 2008 8:05 am

reading text-file

Post 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?
:(
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: reading text-file

Post by papa »

Code: Select all

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