Download file from URL and save locally using PHP

0
1404
Download file from URL and save locally using PHP

PHP is a very supportive programming language and it provides several ways for developers to download file from URL using PHP. I will show you how to do that in PHP.

Download file from URL in PHP

In general, as far as I know, there are three ways to download remote files from Internet, and they can be done by:

  • Using fopen()
  • Using file_get_contents()
  • Using cUrl module.

We will go through each method right below.

Method 1: Using fopen()

According to the PHP documentation for fopen, the input is in form of scheme:// for a list of supported protocols, and it includes http, ftp, file, ssh2

It means we can use fopen to access any file being served on webservers.

This is example usage of fopen() to download file from a remote URL.

const BUFFER = 1024;

function download($remoteFile, $localFile) {
    $fremote = fopen($remoteFile, 'rb');
    if (!$fremote) {
        return false;
    }

    $flocal = fopen($localFile, 'wb');
    if (!$flocal) {
        fclose($fremote);
        return false;
    }

    while ($buffer = fread($fremote, BUFFER)) {
        fwrite($flocal, $buffer);
    }

    fclose($flocal);
    fclose($fremote);

    return true;
}

download(
    'https://raw.githubusercontent.com/petehouston/php-tips/master/README.md',
    'README.md'
);

After opening a reading stream with fopen , we need to create a writing stream to save file to local as you can see from the code above.

Method 2: Using file_get_contents()

Similar to the fopen() function, but instead of handling manually to get remote file contents by creating file pointers, file_get_contents() function will do that for us, it reads whole file contents and stores into a string. And we can write this string into a local file.

$contents = file_get_contents(
    'https://raw.githubusercontent.com/petehouston/php-tips/master/README.md'
);

file_put_contents(
    'README.md',
    $contents
);

Comparing to method 1 of using fopen() , using file_get_contents() is shorter and quicker. However, you should only use this function if you know the remote file is a text file. If remote file is a binary file, you should use fopen().

Method 3: Using cURL module

cURL is an official supported library on PHP, which helps to make requests and handle response to server.

There are 4 steps to use CURL.

  1. Init an instance of cURL.
  2. Config the instance options.
  3. Make request.
  4. Clean up.

To download remote file from an URL, this is basically what you need to do:

$ch = curl_init('https://raw.githubusercontent.com/petehouston/php-tips/master/README.md');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
curl_close($ch);
file_put_contents(
    'README.md',
    $response
);

Let me explain the two options being used in the code.

  • CURLOPT_RETURNTRANSFER : this will make CURL instance to return content of remote file.
  • CURLOPT_SSL_VERIFYPEER : to disable the SSL handshake and verification.

Which one should I use?

It depends on your situations. CURL can perform like both fopen and file_get_contents, it is for a general purpose when you need to config many options for download, such as: enforce SSL verification, request with basic authentication

CURL is an additional module in PHP, so you need to enable CURL module in your php.ini file to use. Additionally, cURL instance cost more memory and initialization duration before making request is longer than using the other two functions.

If you want a simple and lightweight method to download file, use fopen or file_get_contents. Make sure to handle if errors happen.

Summary

As you see, it is easy to download file from URL and save locally using PHP, and it is all depending on your situation to decide which one to use best.

All the demo code for this tutorial is saved on Github, https://github.com/petehouston/php-tips/tree/master/01-download-remote-file

Check it out if you have any problem with the code. Have fun!