Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP - Chunked file copy (via FTP) has missing bytes?
    primarykey
    data
    text
    <p>So, I'm writing a chunked file transfer script that is intended to copy files--small and large--to a remote server. It almost works fantastically (and did with a 26 byte file I tested, haha) but when I start to do larger files, I notice it isn't quite working. For example, I uploaded a 96,489,231 byte file, but the final file was 95,504,152 bytes. I tested it with a 928,670,754 byte file, and the copied file only had 927,902,792 bytes.</p> <p>Has anyone else ever experienced this? I'm guessing <code>feof()</code> may be doing something wonky, but I have no idea how to replace it, or test that. I commented the code, for your convenience. :)</p> <pre><code>&lt;?php // FTP credentials $server = CENSORED; $username = CENSORED; $password = CENSORED; // Destination file (where the copied file should go) $destination = "ftp://$username:$password@$server/ftp/final.mp4"; // The file on my server that we're copying (in chunks) to $destination. $read = 'grr.mp4'; // If the file we're trying to copy exists... if (file_exists($read)) { // Set a chunk size $chunk_size = 4194304; // For reading through the file we want to copy to the FTP server. $read_handle = fopen($read, 'rb'); // For appending to the destination file. $destination_handle = fopen($destination, 'ab'); echo '&lt;span style="font-size:20px;"&gt;'; echo 'Uploading.....'; // Loop through $read until we reach the end of the file. while (!feof($read_handle)) { // So Rackspace doesn't think nothing's happening. echo PHP_EOL; flush(); // Read a chunk of the file we're copying. $chunk = fread($read_handle, $chunk_size); // Write the chunk to the destination file. fwrite($destination_handle, $chunk); sleep(1); } echo 'Done!'; echo '&lt;/span&gt;'; } fclose($read_handle); fclose($destination_handle); ?&gt; </code></pre> <h2>EDIT</h2> <p>I (may have) confirmed that the script is dying at the end somehow, and not corrupting the files. I created a simple file with each line corresponding to the line number, up to 10000, then ran my script. It stopped at line 6253. However, the script is still returning "Done!" at the end, so I can't imagine it's a timeout issue. Strange!</p> <h2>EDIT 2</h2> <p>I have confirmed that the problem exists somewhere in <code>fwrite()</code>. By echoing <code>$chunk</code> inside the loop, the complete file is returned without fail. However, the written file still does not match.</p> <h2>EDIT 3</h2> <p>It appears to work if I add sleep(1) immediately after the fwrite(). However, that makes the script take a million years to run. Is it possible that PHP's append has some inherent flaw?</p> <h2>EDIT 4</h2> <p>Alright, further isolated the problem to being an FTP problem, somehow. When I run this file copy locally, it works fine. However, when I use the file transfer protocol (line 9) the bytes are missing. This is occurring despite the binary flags the two cases of <code>fopen()</code>. What could possibly be causing this?</p> <h2>EDIT 5</h2> <p>I found a fix. The modified code is above--I'll post an answer on my own as soon as I'm able.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload