0

I need my server to download a file and write it to the server's filesystem. The problem is that all the PHP examples I've seen download the file into server RAM and then write to a file.

Since I'm dealing with large files, I'd like PHP to read the file from the other server and immediately write what it reads to a local file.

hakre
  • 193,403
  • 52
  • 435
  • 836
Robert Louis Murphy
  • 1,558
  • 1
  • 16
  • 29
  • `file_put_contents('/path/to/file', file_get_contents('http://example.com/file'));`? – gen_Eric Nov 16 '12 at 19:15
  • You have not shared to which examples you related to, but on this website we have QA material that shows how to not use a RAM buffer that is as large as the total file-size. Also the PHP manual is full of functions and examples about that. I really wonder what's standing in your way. http://php.net/filesystem – hakre Nov 17 '12 at 11:08
  • @jtheman - I'm using windows on Amazon EC2 Servers, we are getting ready to switch to Linux. Unfortunately, we can connect to some FTP servers, but other FTP servers can't make the data channel connection back through the NAT server, and won't let us use passive connections, so for this one, we've done it via PHP/HTTP using Kamil's example below. If I had full control over the other server, I'd install an FTP server that a client running on EC2 can connect to. – Robert Louis Murphy Nov 20 '12 at 13:34

1 Answers1

4

Use curl like this:

<?php
 $url  = 'http://www.example.com/a-large-file.zip';
 $path = '/path/to/a-large-file.zip';

 $fp = fopen($path, 'w');

 $ch = curl_init($url);
 curl_setopt($ch, CURLOPT_FILE, $fp);

 $data = curl_exec($ch);

 curl_close($ch);
 fclose($fp);
?>
Kamil Šrot
  • 2,141
  • 17
  • 19