bigLarry bigLarry - 5 months ago 41
PHP Question

Need php script to download a file on a remote server and save locally

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?

Answer
$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);
Comments