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?

animuson
  • 53,861
  • 28
  • 137
  • 147
Ralph Canapa
  • 610
  • 1
  • 5
  • 21

7 Answers7

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);
Parris Varney
  • 11,320
  • 12
  • 47
  • 76
  • Thank you. I'm trying it now but it seems that the browser hangs. – Ralph Canapa Dec 21 '10 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 '10 at 21:56
  • For bigger files use this [http://stackoverflow.com/a/3938564/1057527](http://stackoverflow.com/a/3938564/1057527) – machineaddict May 23 '13 at 10:15
  • Better one-line solution below: https://stackoverflow.com/a/37726077/179571 – Scott Feb 08 '18 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? – ryanmattscott Jun 13 '18 at 20:22
  • How to download files with this URL: https://filehippo.com/download_mozilla-firefox-64/post_download/ – Saurin Dashadia Jul 10 '20 at 13:36
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'));
Kuldeep
  • 202
  • 3
  • 8
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");
Blagovest Buyukliev
  • 42,498
  • 14
  • 94
  • 130
  • It is very useful on hosts that support SSH. Just if you want to get some file from remote server to another server `wget http://public-url-of-the-file` – SaidbakR Nov 29 '15 at 20:52
  • What about the URL like this? : https://filehippo.com/download_mozilla-firefox-64/post_download/ – Saurin Dashadia Jul 10 '20 at 13:34
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);
?>     
Hbirjand
  • 1,945
  • 19
  • 25
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'))
Salar
  • 5,305
  • 7
  • 50
  • 78
0

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

It have progress_bar and simple filename detactor...

aditya7822
  • 23
  • 4
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);

?>
Saeed Sepehr
  • 93
  • 1
  • 7
  • What if URL is: https://filehippo.com/download_mozilla-firefox-64/post_download/ Please check this: https://stackoverflow.com/questions/62835395/how-to-download-file-using-php-that-has-delayed-force-download – Saurin Dashadia Jul 10 '20 at 13:32