note 20517 added to function.fgets

From: Date: Sat, 06 Apr 2002 08:00:49 +0000
Subject: note 20517 added to function.fgets
Groups: php.notes 
Request: Send a blank email to php-notes+get-28692@lists.php.net to get a copy of this message
fgets() is evil. If you are doing cross platform development where your PHP programs need to run on windows, unix and macs, or even are reading from files that were developed on various platforms, do not (I repeat DO NOT) use fgets(). This is because fgets(), as mentioned before, does not deal with carriage returns and line feeds for all systems. For instance, this program will only behave properly if line feeds exist. So if it comes across a carriage return and then doesn't find a line feed, it will continue scanning the file and add characters before it returns. For programs that rely on newlines being there, they simply will not work. Solution: Use fread to get a buffer of data and test for the carriage returns and newlines yourself. Basically, if you spot a carriage return, return the new line. If a line feed happens to exist, simple throw it away. If there is no carriage return, then look for a line feeds only. This is the safest way to read from files. -- http://www.php.net/manual/en/function.fgets.php http://master.php.net/manage/user-notes.php?action=edit+20517 http://master.php.net/manage/user-notes.php?action=delete+20517 http://master.php.net/manage/user-notes.php?action=reject+20517

« previous php.notes (#28692) next »