18

Trying to download a file on a remote server and save it to a local subdirectory.

The following code seems to work for small files, < 1MB, but larger files just time out and don't even begin to download.

<?php

 $source = "http://someurl.com/afile.zip";
 $destination = "/asubfolder/afile.zip";

 $data = file_get_contents($source);
 $file = fopen($destination, "w+");
 fputs($file, $data);
 fclose($file);

?>

Any suggestions on how to download larger files without interruption?

1
  • Thank you for your reply. I don't have curl on the system hosting the script. Dec 21, 2010 at 21:50

7 Answers 7

33
$ch = curl_init();
$source = "http://someurl.com/afile.zip";
curl_setopt($ch, CURLOPT_URL, $source);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec ($ch);
curl_close ($ch);

$destination = "/asubfolder/afile.zip";
$file = fopen($destination, "w+");
fputs($file, $data);
fclose($file);
6
  • Thank you. I'm trying it now but it seems that the browser hangs. Dec 21, 2010 at 21:53
  • @bigLarry you gotta wait for the file to download, the browser might hang till the download finishes. Try with a medium size file which will finish faster but is big enough to hit the timeout
    – tHeSiD
    Dec 21, 2010 at 21:56
  • For bigger files use this http://stackoverflow.com/a/3938564/1057527 May 23, 2013 at 10:15
  • Better one-line solution below: stackoverflow.com/a/37726077/179571
    – Scott
    Feb 8, 2018 at 21:48
  • When I run this I get the file to show up, but with a size of 0KB. The other methods give me bad address errors or 502 Gateway issues. Any ideas? Jun 13, 2018 at 20:22
5

file_get_contents shouldn't be used for big binary files because you can easily hit PHP's memory limit. I would exec() wget by telling it the URL and the desired output filename:

exec("wget $url -O $filename");
2
5

Since PHP 5.1.0, file_put_contents() supports writing piece-by-piece by passing a stream-handle as the $data parameter:

file_put_contents("Tmpfile.zip", fopen("http://someurl/file.zip", 'r'));
1
  • This is an AWESOME solution - works fast and simple - THANKS!
    – Scott
    Feb 8, 2018 at 21:47
3

I always use this code,it's working very well.

<?php
define('BUFSIZ', 4095);
$url = 'Type The URL Of The File';
$rfile = fopen($url, 'r');
$lfile = fopen(basename($url), 'w');
while(!feof($rfile))
fwrite($lfile, fread($rfile, BUFSIZ), BUFSIZ);
fclose($rfile);
fclose($lfile);
?>     
1
  • next level answer
    – Mojtaba Hn
    Jun 20, 2019 at 12:49
1

Use this solution if you do not know the format of the file that you are going to download.

$url = 'http:://www.sth.com/some_name.format' ;
$parse_url = parse_url($url) ;
$path_info = pathinfo($parse_url['path']) ;
$file_extension = $path_info['extension'] ;
$save_path = 'any/local/path/' ;
$file_name = 'name' . "." . $file_extension ;
file_put_contents($save_path . $file_name , fopen($url, 'r'))
0

Try out phpRFT:http://sourceforge.net/projects/phprft/files/latest/download?source=navbar

It have progress_bar and simple filename detactor...

0

A better and lighter script which is streaming file:

<?php

$url  = 'http://example.com/file.zip'; //Source absolute URL
$path = 'file.zip'; //Patch & file name to save in destination (currently beside of PHP script file)

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

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

$data = curl_exec($ch);

curl_close($ch);
fclose($fp);

?>
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.