#23406 [Opn->Ana]: \r\n not functioning the same from 4.3.1 and prior

From: Date: Thu, 01 May 2003 14:11:50 +0000
Subject: #23406 [Opn->Ana]: \r\n not functioning the same from 4.3.1 and prior
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-38880@lists.php.net to get a copy of this message
ID: 23406 Updated by: wez@php.net Reported By: gfraley5 at earthlink dot net -Status: Open +Status: Analyzed Bug Type: Filesystem function related Operating System: Win98SE,WinXP,Win2K PHP Version: 4.3.2RC2 Assigned To: wez New Comment: The Apache SAPI leaves the _fmode variable as-is, whereas the CLI, CGI and Embed SAPIs all force it to binary mode. What this means is that for the latter three, fopen($file, "r") is forced to fopen($file, "rb") internally by the MS libc. Technically speaking this behaviour is a bit naughty, as it prevents opening files in text-mode when using CLI and CGI, and Embed interferes with the hosting application's binary settings (this is the main reason we don't set it in Apache). I can't find any logical reason for the apparent change in behaviour under Apache, except for the fact that we are now using the lower level file descriptors returned by _open() rather than calling the slightly higher level fopen() to get at the files. Why this makes a difference, I can't tell (it just doesn't make sense). However, all of this aside, the behaviour you are currently experiencing under Apache is the technically correct behaviour that you should expect when opening a file in text mode. The correct fix for your problem is to open the file in binary mode, as is suggested in the PHP manual for the fopen() function, and in numerous places throughout the manual when talking about binary safety. I'm tempted to mark this report as bogus (because it is the expected behaviour), but want to discuss the implications of this with some of the other developers first, because of potential backwards compatibility issues. Previous Comments: ------------------------------------------------------------------------ [2003-04-30 13:58:38] gfraley5 at earthlink dot net Apache ------------------------------------------------------------------------ [2003-04-30 13:18:02] wez@php.net Which SAPI are you using? CGI/FastCGI, Apache or IIS? ------------------------------------------------------------------------ [2003-04-30 12:06:54] gfraley5 at earthlink dot net From 4.2.3 Wrote: 76 bytes; filesize is now 80 ==== using readfile ===== msgid 1049987091 1050930128 ==== readfile read 80 bytes ==== ==== using fopen 'r' === msgid 1049987091 1050930128 ==== using fopen 'rb' === msgid 1049987091 1050930128 ==== done === From 4.3.2 Wrote: 76 bytes; filesize is now 77 ==== using readfile ===== msgid 1049987091 1050930128 ==== readfile read 77 bytes ==== ==== using fopen 'r' === msgid 1049987091 1050930128 ==== using fopen 'rb' === msgid 1049987091 1050930128 ==== done === ------------------------------------------------------------------------ [2003-04-30 06:24:14] wez@php.net I can't reproduce this here (tried 4.2.1 and 4.3.2 under XP). NOTE: readfile() returns the number of bytes it wrote, NOT the contents of the file. Use file_get_contents() for that. Please try this script on 4.3.2 and your 4.2.3 and report back (paste the output from each into this report): <?php $myArray[] = 'msgid'."\r\n"; $myArray[] = '1049987091'."\r\n"; $myArray[] = '1050930128'."\r\n"; $l = 0; $fp = fopen("problemFile.php",'w') or die("Unable to open file"); for ($i=0;$i<count($myArray);$i++) { if ($i==0) { $prefx = "<? Header(\"HTTP/1.0 403 Forbidden\");exit;?>\r\n"; } else { $prefx = ""; } $l += fwrite($fp, $prefx.$myArray[$i]); } fclose($fp); echo "Wrote: $l bytes; filesize is now " . filesize("problemFile.php") . "\n"; echo "==== using readfile =====\n"; $x = readfile("problemFile.php"); echo "==== readfile read $x bytes ====\n"; echo "==== using fopen 'r' ===\n"; $fp = fopen("problemFile.php", "r"); while (!feof($fp)) { echo fgets($fp); } fclose($fp); echo "==== using fopen 'rb' ===\n"; $fp = fopen("problemFile.php", "rb"); while (!feof($fp)) { echo fgets($fp); } fclose($fp); echo "==== done ===\n"; ?> ------------------------------------------------------------------------ [2003-04-30 05:57:42] sniper@php.net Wez: You broke it, fix it before 4.3.2 goes gold. (some streams stuff :) ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at http://bugs.php.net/23406 -- Edit this bug report at http://bugs.php.net/?id=23406&edit=1

« previous php.bugs (#38880) next »