Jump to content

[SOLVED] stream_get_line works... fgets doesn't


heavyEddie

Recommended Posts

I'm trying to use the code below to parse some information in a text file that is generated on a remote server.  If I use stream_get_line it works nicely, but fgets simply gets stuck in an endless loop.  Any ideas?

 

I wish to use fgets since it appears that stream_get_line isn't available on many hosts.

 

$file = "http://www.site.com/bla.pl?id=1";
$handle = fopen($file, "r");

// Read each line of the file and check to see if this person is still logged in or not.
if ($handle) {
    while (!feof($handle)) {
    	  //$buffer = stream_get_line($handle, 1000, "\n");
    	  $buffer = fgets($handle, 4096, "\n");
    	  //bla bla bla bla bla
     }
    fclose($handle);
}

Why do you have three arguments in fgets()? It should only accept two. If you just put the one argument ($handle) it will read to the end of the line, unless, for some reason, you only want the first 4096 bytes of a line.

 

However, if you are just putting the contents of the file into a buffer string, why don't you just use file_get_contents()? It is the most efficient functions this sort of thing.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.