|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2005-10-14 13:32 UTC] tony2001@php.net
[2005-10-14 14:50 UTC] aaron-php at oakadaptive dot com
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Nov 25 15:00:01 2025 UTC |
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)