title: How to copy very large files from URL to server via PHP?
body: <p>I use the following code to copy/download files from an external server (any server via a URL) to my hosted web server(Dreamhost shared hosting at default settings). </p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;form method="post" action="copy.php"&gt;
      &lt;input type="submit" value="click" name="submit"&gt;
  &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  &lt;!-- copy.php file contents --&gt;
  &lt;?php
  function chunked_copy() {
      # 1 meg at a time, adjustable.
      $buffer_size = 1048576; 
      $ret = 0;
      $fin = fopen("http://www.example.com/file.zip", "rb");
      $fout = fopen("file.zip", "w");
      while(!feof($fin)) {
          $ret += fwrite($fout, fread($fin, $buffer_size));
      }
      fclose($fin);
      fclose($fout);
      return $ret; # return number of bytes written
  }
  if(isset($_POST['submit']))
  {
     chunked_copy();
  } 
  ?&gt;
  </code></pre>
  
  <p>However the function stops running at about once 2.5GB (sometimes 2.3GB and sometimes 2.7GB, etc) of the file has downloaded. This happens every time I execute this function. Smaller files (&lt;2GB) rarely exhibit this problem. I believe nothing is wrong with the source as I separately downloaded the file flawlessly onto my home PC.</p>
  
  <p>Can someone please remedy and explain this problem to me? I am very new to programming.</p>
  
  <p>Also,</p>
  
  <pre><code>file_put_contents("Tmpfile.zip", fopen("http://example.com/file.zip", 'r')); 
  </code></pre>
  
  <p>exhibits similar symptoms as well.</p>