Last Updated : 18 Sep, 2024
Summarize
Comments
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.
Below are the approaches to download file from URL using PHP:
Table of Content
- Using file_get_contents() function
- Using PHP Curl
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: This example illustrates the use of file_get_contents() function to read the file into a string.
<?php // Initialize a file URL to the variable $url = 'https://media.geeksforgeeks.org/wp-content/uploads/gfg-40.png'; // 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:
php source folder
After running the program:
File downloaded after successful execution
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 // Initialize a file URL to the variable $url = 'https://media.geeksforgeeks.org/wp-content/uploads/gfg-40.png'; // 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:
php source folder
After running the program:
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.
Next Article
How to download File Using JavaScript/jQuery ?
Similar Reads
- How to download File Using JavaScript/jQuery ? The ability to download files directly from a website is an essential feature for many web applications. Whether you're providing documents, images, or other data, enabling users to download files seamlessly enhances their experience and ensures they can access the content offline. This article prov 2 min read
- How to Download a File Using Node.js? Downloading files from the internet is a common task in many Node.js applications, whether it's fetching images, videos, documents, or any other type of file. In this article, we'll explore various methods for downloading files using Node.js, ranging from built-in modules to external libraries. Usin 3 min read
- How to unzip a file using PHP ? To unzip a file with PHP, we can use the ZipArchive class. ZipArchive is a simple utility class for zipping and unzipping files. We don't require any extra additional plugins for working with zip files. ZipArchive class provides us a facility to create a zip file or to extract the existing zip file. 2 min read
- How to Download a File using Express.js ? Express.js is a routing and middleware framework for handling the different routing of the webpage and it works between the request and response cycle and works on the top of the node.js server. In this article, we will discuss how to download a file using express.js. ApproachTo download a file usin 3 min read
- How to download Google Images using Python Python is a multi-purpose language and widely used for scripting. We can write Python scripts to automate day-to-day things. Letâs say we want to download google images with multiple search queries. Instead of doing it manually we can automate the process. How to install needed Module : pip install 2 min read
- Download Any File From URL with Vanilla JavaScript Downloading files from a URL using vanilla JavaScript involves creating a link element, setting its href attribute to the file URL, and programmatically triggering a click event. This method allows users to download files without relying on external libraries or frameworks. Downloading files from a 3 min read
- How to force a file to get downloaded in PHP? To force a file to get downloaded, there is no need to use the PHP script if you want to keep it simple. If such kind of file is stored in a publicly accessible folder, just create a hyperlink pointing to that file. When the user clicks the file link then the file will be downloaded. You can check t 2 min read
- How to enable file downloads from Express Routes ? In web development, lets users to download files from the server through an Express route is a common requirement. Express provides straightforward methods to do this process efficiently. In this article, you will see how can you enable file downloads from an Express route. Prerequisites:Express JSP 2 min read
- How to download PDF file in ReactJS ? To download pdf in React JS there are methods given in JavaScript and HTML DOM. They provide a convenient way to download files and store them in local computers. Here we will use locally stored pdf files in the project folder to use as a sample pdf downloaded by the browser. These are the approache 3 min read