Skip to content

Instantly share code, notes, and snippets.

@damienalexandre
Created October 3, 2011 09:47
Show Gist options
  • Star 33 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save damienalexandre/1258787 to your computer and use it in GitHub Desktop.
Save damienalexandre/1258787 to your computer and use it in GitHub Desktop.
Download large file from the web via php
<?php
/**
* Download a large distant file to a local destination.
*
* This method is very memory efficient :-)
* The file can be huge, PHP doesn't load it in memory.
*
* /!\ Warning, the return value is always true, you must use === to test the response type too.
*
* @author dalexandre
* @param string $url
* The file to download
* @param ressource $dest
* The local file path or ressource (file handler)
* @return boolean true or the error message
*/
public static function downloadDistantFile($url, $dest)
{
$options = array(
CURLOPT_FILE => is_resource($dest) ? $dest : fopen($dest, 'w'),
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_URL => $url,
CURLOPT_FAILONERROR => true, // HTTP code > 400 will throw curl error
);
$ch = curl_init();
curl_setopt_array($ch, $options);
$return = curl_exec($ch);
if ($return === false)
{
return curl_error($ch);
}
else
{
return true;
}
}
@faca5
Copy link

faca5 commented Feb 2, 2018

Excellent!

@namikiri
Copy link

Did you forget fclose($dest)?

@snk-max
Copy link

snk-max commented Sep 9, 2019

Dont work for large files, i got "Maximum execution time of 30 seconds exceeded"

@Cankar001
Copy link

Thank you very much! This helped a lot!

@rohjay
Copy link

rohjay commented Mar 25, 2020

Dont work for large files, i got "Maximum execution time of 30 seconds exceeded"

That's most likely an issue with your php.ini. Loook for the setting called "max_execution_time" - defaults to 30, IIRC. You can set it to 0 (zero) to disable this safety feature altogether (would not recommend on live servers).

@yair-man
Copy link

yair-man commented Jun 2, 2020

I get error 502 is there a way to overcome it?

@dovi5988
Copy link

I get error 502 is there a way to overcome it?

That is coming from the remote server. You need to ask them why they are sending a 502.

@eviewer-uk
Copy link

Great snippet, saved me a bunch of time transferring files between servers, good job! Transferred 1.2Gb file in around 30 seconds

@littleomie
Copy link

Nicely done. Thanks mate!

@maximsymfony
Copy link

maximsymfony commented Apr 4, 2023

    public static function uploadFromRemoteServer(string $remoteFileURL, mixed $destinationFilepath): string|bool
    {
        $options = [
            CURLOPT_URL             => $remoteFileURL,
            CURLOPT_FILE            => is_resource($destinationFilepath) ? $destinationFilepath : fopen($destinationFilepath, 'wb'),
            CURLOPT_FOLLOWLOCATION  => true,
            CURLOPT_FAILONERROR     => true,
        ];

        $curl           = curl_init();
        curl_setopt_array($curl, $options);
        $returnResult   = curl_exec($curl);

        return (false === $returnResult) ? curl_error($curl) : true;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment