From: karachi at mail dot ru Operating system: FreeBSD PHP version: 5.2.10 PHP Bug Type: Streams related Bug description: fwrite() doesn't check reply from ftp server before exiting
Description: ------------ I tried to upload a file to ftp server using ftp wrapper functions. Sequence of steps are: fopen, fwrite, fclose. Sometimes file uploaded successfully and sometimes several last bytes of file didn't save. Server reports in log file that transmission was aborted, but fwrite() returns "true". I found that fwrite() doesn't check ftp reply code before exiting. That's why ftp server can receive QUIT command before it processes data stored and close data connection as described in RFC 959: "The server may abort data transfer if the control connections are closed without command." If I insert sleep(1) between fwrite() and fclose() everything works as expected. Ftp server used: ProFTPD 1.3.2 Reproduce code: --------------- <?php $host = 'localhost'; $user = 'ftp'; $passwd = 'anonymous'; $f = fopen("/home/user/test/test_file", 'r') or die ("Unable to open file"); $context = stream_context_create( array( "ftp" => array( "overwrite" => true ) ) ); $f1 = fopen("ftp://$host/test", 'wb', false, $context) or die("Unable to open the file on ftp"); while ($str = fread($f, 1000)) { fwrite($f1, $str) or die("Unable to write"); } fclose($f1); fclose($f); ?> Expected result: ---------------- File is successfully stored on ftp server Actual result: -------------- Sometimes end of the file isn't stored. It doesn't depend on the size of the file. -- Edit bug report at http://bugs.php.net/?id=48607&edit=1 -- Try a CVS snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=48607&r=trysnapshot52 Try a CVS snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=48607&r=trysnapshot53 Try a CVS snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=48607&r=trysnapshot60 Fixed in CVS: http://bugs.php.net/fix.php?id=48607&r=fixedcvs Fixed in CVS and need be documented: http://bugs.php.net/fix.php?id=48607&r=needdocs Fixed in release: http://bugs.php.net/fix.php?id=48607&r=alreadyfixed Need backtrace: http://bugs.php.net/fix.php?id=48607&r=needtrace Need Reproduce Script: http://bugs.php.net/fix.php?id=48607&r=needscript Try newer version: http://bugs.php.net/fix.php?id=48607&r=oldversion Not developer issue: http://bugs.php.net/fix.php?id=48607&r=support Expected behavior: http://bugs.php.net/fix.php?id=48607&r=notwrong Not enough info: http://bugs.php.net/fix.php?id=48607&r=notenoughinfo Submitted twice: http://bugs.php.net/fix.php?id=48607&r=submittedtwice register_globals: http://bugs.php.net/fix.php?id=48607&r=globals PHP 4 support discontinued: http://bugs.php.net/fix.php?id=48607&r=php4 Daylight Savings: http://bugs.php.net/fix.php?id=48607&r=dst IIS Stability: http://bugs.php.net/fix.php?id=48607&r=isapi Install GNU Sed: http://bugs.php.net/fix.php?id=48607&r=gnused Floating point limitations: http://bugs.php.net/fix.php?id=48607&r=float No Zend Extensions: http://bugs.php.net/fix.php?id=48607&r=nozend MySQL Configuration Error: http://bugs.php.net/fix.php?id=48607&r=mysqlcfg