title: php fwrite() doesn't finish writing string data to file, why?
body: <p>I'm trying to write a sizable chunk of data to a file that is opened via fopen() in php.  The protocol wrapper I'm using is ftp, so the file is remote to the server running the php code.  The file I'm writing to is on a Windows server.</p>
  
  <p>I verified that the file does, in fact, get created by my php code, but the problem is that the data within the file is either non-existant (0KB) or writing to the file stops prematurely.  Not sure why this is the case.  </p>
  
  <p>Here is the code I am using for handling the operation:</p>
  
  <pre><code>$file_handle = fopen($node['ftp'].$path_to_lut, "wb", 0, $node['ftp_context']);
  include_once($file);
  
  if ($file_handle)
  {
       fwrite($file_handle, $string);  //$string is inside included $file
       fclose($file_handle);
  } else {
       die('There was a problem opening the file.');
  }
  </code></pre>
  
  <p>This code works fine when I host it on my local machine, but when I upload it to my webhost (Rackspace Cloud), it fails.  This leads me to believe it's an issue related to the configuration of the my server at Rackspace, but want to know if there is anything I can do to my php code to make it more robust.</p>
  
  <p>Any ideas to ensure fwrite actually finishes writing the string to the remote machine?</p>
  
  <p>Thanks!</p>
  
  <p>Okay, I changed the code that writes to the file like so:</p>
  
  <pre><code>if ($file_handle)
  {
      if ($bytesWritten = fwrite($file_handle, $string) ) {
          echo "There were " . $bytesWritten . " bytes written to the text file.";
      }
  
      if (!fflush($file_handle)) {
          die("There was a problem outputting all the data to the text file.");
      }
  
      if (!fclose($file_handle)) { 
          die("There was a problem closing the text file."); 
      }
  
  } else {
  
      die("No file to write data to.  Sorry.");
  
  }
  </code></pre>
  
  <p>What is strange is that the echo statement shows the following:</p>
  
  <blockquote>
    <p>There were 10330 bytes written to the text file.</p>
  </blockquote>
  
  <p>And yet, when I verify the text file size via FTP it shows it to be 0K and the data inside the file is, in fact, truncated.  I can't imagine it has to do with the FTP server itself because it works if the PHP is hosted on a machine other than the one on Rackspace Cloud.  </p>
  
  <p>** UPDATE **
  I spoke to a Rackspace Cloud rep who mentioned that they require passive ftp if you're going to ftp from their servers.  I setup the remote server to handle passive ftp connections, and have verified that passive ftp now works on the remote server via the OSX Transmit ftp client.  I added:</p>
  
  <pre><code>ftp_pasv($file_handle, true);
  </code></pre>
  
  <p>Right after the fopen() statement, but I get an error from PHP saying the I didn't provide a valid resource to ftp_pasv().  How can I ensure that the connection to the ftp site that PHP makes is PASV and not ACTIVE and still use fwrite()?  Incidentally, I've noticed that the Windows machine reports that the file being written by my PHP code is 4096 bytes on disk.  It never gets beyond that amount.  This led me to change the output_buffering php value to 65536 just to troubleshoot, but that didn't fix the issue either. . .</p>
  
  <p>** UPDATE PART DUEX **</p>
  
  <p>Troubleshooting the problem on the my virtual server on the Rackspace Cloud Sites product was proving too difficult because they don't offer enough admin rights.  I created a very small cloud server on Rackspace's Cloud Server product and configured everything to the point where I'm still seeing the same error with fwrite().  To make sure that I could write a file from that server to a remote server, I used basic ftp commands within my bash shell on the cloud server.  It worked fine.  So, I assume that there is a bug within the php implementation of fwrite(), and that it is probably due to some type of data throttling issue.  When I write to the remote server from my local environment which has a slow upspeed compared to what is offered on the Rackspace Cloud server, it works fine.  Is there any way to effectively throttle down the speed of the write?  Just askin' :)</p>
  
  <p>** UPDATE PART III <em>*</em></p>
  
  <p>So, I took the suggestion from @a sad dude and implemented a function that might help somebody trying to write to a new file and send it off in its entirety via ftp:</p>
  
  <pre><code>function writeFileAndFTP($filename=null, $data=null, $node=null, $local_path=null, $remote_path=null)
  {
  
      //  !Determin the path and the file to upload from the webserver
      $file = $local_path.'/'.$filename;
  
  
      //  !Open a new file to write to on the local machine
      if (!($file_handle = fopen($file, "wb", 0))) { 
          die("There was a problem opening ".$file." for writing!");
      }
  
  
      //  !Write the file to local disk
      if ($bytesWritten = fwrite($file_handle, $data) ) {
          //echo "There were " . $bytesWritten . " bytes written to " . $file;
      }
  
      //  !Close the file from writing
      if (!fclose($file_handle)) {
          die("There was a problem closing " . $file);
      }
  
      //  !Create connection to remote FTP server
      $ftp_cxn = ftp_connect($node['addr'], $node['ftp_port']) or die("Couldn't connect to the ftp server.");
  
      //  !Login to the remote server
      ftp_login($ftp_cxn, $node['user'], getPwd($node['ID'])) or die("Couldn't login to the ftp server.");
  
      //  !Set PASV or ACTIVE FTP
      ftp_pasv($ftp_cxn, true);
  
  
      //  !Upload the file
      if (!ftp_put($ftp_cxn, $remote_path.'/'.$filename, $file, FTP_ASCII)) {
          die("There was an issue ftp'ing the file to ".$node['addr'].$remote_path);  
      }
  
      //  !Close the ftp connection
      ftp_close($ftp_cxn);
  
  }
  </code></pre>