From: aaron-php at oakadaptive dot com Operating system: Linux (Red Hat and SUSE) PHP version: 5.0.5 PHP Bug Type: Filesystem function related Bug description: fwrite returns 0 instead of false on some failures
Description: ------------ When fwrite can't write its whole second argument, it returns the number of bytes it did succeed in writing instead of false. This behavior is mentioned in the user comments for fwrite, but I couldn't find it reported as a bug. I consider it a bug, because it makes testing for a failed fwrite harder; it's necessary to compare the length of the string with fwrite's return value. If it is intended behavior, I think the documentation should be clearer, explicitly saying that if fwrite does return an int, it can be less than the string's length (and as little as zero). (fwrite does return false if, for instance, its first argument is not a file handle. It also triggers a warning.) Reproduce code: --------------- $Filename = 'deleteme'; if (touch($Filename)): // Make sure file exists. $FileHnd = fopen($Filename, 'r'); // Open read-only. if ($FileHnd): $ByteCount = fwrite($FileHnd, 'test'); // Try to write 4 bytes. if ($ByteCount === false): echo "fwrite() returned false\n"; else: echo "fwrite() wrote $ByteCount byte(s)\n"; // Reports // that 0 bytes were written (tested on v. 5.0.5 and 4.3.4): endif; fclose($FileHnd); unlink($Filename); // Clean up. endif; endif; Expected result: ---------------- fwrite() returned false Actual result: -------------- fwrite() wrote 0 byte(s) -- Edit bug report at http://bugs.php.net/?id=34860&edit=1 -- Try a CVS snapshot (php4): http://bugs.php.net/fix.php?id=34860&r=trysnapshot4 Try a CVS snapshot (php5.0): http://bugs.php.net/fix.php?id=34860&r=trysnapshot50 Try a CVS snapshot (php5.1): http://bugs.php.net/fix.php?id=34860&r=trysnapshot51 Fixed in CVS: http://bugs.php.net/fix.php?id=34860&r=fixedcvs Fixed in release: http://bugs.php.net/fix.php?id=34860&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=34860&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=34860&r=needscript Try newer version: http://bugs.php.net/fix.php?id=34860&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=34860&r=support Expected behavior: http://bugs.php.net/fix.php?id=34860&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=34860&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=34860&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=34860&r=globals PHP 3 support discontinued: http://bugs.php.net/fix.php?id=34860&r=php3 Daylight Savings: http://bugs.php.net/fix.php?id=34860&r=dst IIS Stability: http://bugs.php.net/fix.php?id=34860&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=34860&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=34860&r=float No Zend Extensions: http://bugs.php.net/fix.php?id=34860&r=nozend MySQL Configuration Error: http://bugs.php.net/fix.php?id=34860&r=mysqlcfg