note 92397 modified in function.fgets by danbrown

From: Date: Thu, 23 Jul 2009 01:19:52 +0000
Subject: note 92397 modified in function.fgets by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-158602@lists.php.net to get a copy of this message
One thing I discovered with fgets, at least with PHP 5.1.6, is that you may have to use an IF statement to avoid your code running rampant (and possibly hanging the server). This can cause problems if you do not have root access on the server on which you are working. This is the code I have implemented ($F1 is an array): <?php if($fh = fopen("filename","r")){ while (!feof($fh)){ $F1[] = fgets($fh,9999); } fclose($fh); } ?> I have noticed that without the IF statement, fgets seems to ignore when $fh is undefined (i.e., "filename" does not exist). If that happens, it will keep attempting to read from a nonexistent filehandle until the process can be administratively killed or the server hangs, whichever comes first. --was-- One thing I discovered with fgets, at least with PHP 5.1.6, is that you may have to use an IF statement to avoid your code running rampant (and possibly hanging the server). This can cause problems if you do not have root access on the server on which you are working. This is the code I have implemented ($F1 is an array): <?php if($fh = fopen("filename","r")){ while (!feof($fh)){ $F1[] = fgets($fh,9999); } fclose($fh); ?> I have noticed that without the IF statement, fgets seems to ignore when $fh is undefined (i.e., "filename" does not exist). If that happens, it will keep attempting to read from a nonexistent filehandle until the process can be administratively killed or the server hangs, whichever comes first. http://php.net/manual/en/function.fgets.php

« previous php.notes (#158602) next »