Download file from URL using PHP - GeeksforGeeks (2024)

Table of Contents
PHP PHP Please Login to comment...

Improve

In this article, we will see how to download & save the file from the URL in PHP, & will also understand the different ways to implement it through the examples. There are many approaches to download a file from a URL, some of them are discussed below:

Using file_get_contents() function: The file_get_contents() function is used to read a file into a string. This function uses memory mapping techniques that are supported by the server and thus enhances the performance making it a preferred way of reading the contents of a file.

Syntax:

file_get_contents($path, $include_path, $context, $start, $max_length)

Example 1: This example illustrates the use of file_get_contents() function to read the file into a string.

PHP

<?php

// Initialize a file URL to the variable

$url =

// Use basename() function to return the base name of file

$file_name = basename($url);

// Use file_get_contents() function to get the file

// from url and use file_put_contents() function to

// save the file by using base name

if (file_put_contents($file_name, file_get_contents($url)))

{

echo "File downloaded successfully";

}

else

{

echo "File downloading failed.";

}

?>

Output:

Before running the program:

Download file from URL using PHP - GeeksforGeeks (1)

php source folder

After running the program:

Download file from URL using PHP - GeeksforGeeks (2)

File downloaded after successful execution

Download file from URL using PHP - GeeksforGeeks (3)

Downloaded image file

Using PHP Curl: The cURL stands for ‘Client for URLs’, originally with URL spelled in uppercase to make it obvious that it deals with URLs. It is pronounced as ‘see URL’. The cURL project has two products libcurl and curl.

Steps to download the file:

  • Initialize a file URL to the variable.
  • Create cURL session.
  • Declare a variable and store the directory name where the downloaded file will save.
  • Use the basename() function to return the file basename if the file path is provided as a parameter.
  • Save the file to the given location.
  • Open the saved file location in write string mode.
  • Set the option for cURL transfer.
  • Perform cURL session and close cURL session and free all resources.
  • Close the file.

Example: This example illustrates the use of the PHP Curl to make HTTP requests in PHP, in order to download the file.

PHP

<?php

// Initialize a file URL to the variable

$url =

// Initialize the cURL session

$ch = curl_init($url);

// Initialize directory name where

// file will be save

$dir = './';

// Use basename() function to return

// the base name of file

$file_name = basename($url);

// Save file into file location

$save_file_loc = $dir . $file_name;

// Open file

$fp = fopen($save_file_loc, 'wb');

// It set an option for a cURL transfer

curl_setopt($ch, CURLOPT_FILE, $fp);

curl_setopt($ch, CURLOPT_HEADER, 0);

// Perform a cURL session

curl_exec($ch);

// Closes a cURL session and frees all resources

curl_close($ch);

// Close file

fclose($fp);

?>

Output:

Before running the program:

Download file from URL using PHP - GeeksforGeeks (4)

php source folder

After running the program:

Download file from URL using PHP - GeeksforGeeks (5)

Downloaded image file

PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.



Last Updated : 17 Jan, 2022

Like Article

Save Article

Share your thoughts in the comments

Please Login to comment...

Download file from URL using PHP - GeeksforGeeks (2024)
Top Articles
Latest Posts
Article information

Author: Domingo Moore

Last Updated:

Views: 6098

Rating: 4.2 / 5 (53 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Domingo Moore

Birthday: 1997-05-20

Address: 6485 Kohler Route, Antonioton, VT 77375-0299

Phone: +3213869077934

Job: Sales Analyst

Hobby: Kayaking, Roller skating, Cabaret, Rugby, Homebrewing, Creative writing, amateur radio

Introduction: My name is Domingo Moore, I am a attractive, gorgeous, funny, jolly, spotless, nice, fantastic person who loves writing and wants to share my knowledge and understanding with you.