- How to Save Image from URL using PHP
- Save Image from URL using PHP
- Save Image from URL using cURL
- How to download file from URL using PHP
- To download file from URL using PHP
- 1. Using PHP file_get_contents() and file_put_contents() function:
- 2. Using PHP CURL and fopen()
- Final thoughts
- You might also like
- References
- Download a file from the URL in PHP
- First Method
- Second Method.
- Save Image From URL in PHP
- Save Image From URL Using file_get_contents() and file_put_contents() in PHP
- Save Image From URL Using cURL
- Save Image From URL Using the copy() Function in PHP
- Save Image From URL Using fread() and fwrite() in PHP
- Save a gzip Image in PHP
- Related Article — PHP Image
How to Save Image from URL using PHP
Saving image from URL is very useful when you want to copy an image dynamically from the remote server and store in the local server. The file_get_contents() and file_put_contents() provides an easiest way to save remote image to local server using PHP. The image file can be saved straight to directory from the URL. In the example code snippet, we will provide two ways to save image from URL using PHP.
Save Image from URL using PHP
The following code snippet helps you to copy an image file from remote URL and save in a folder using PHP.
- file_get_contents() – This function is used to read the image file from URL and return the content as a string.
- file_put_contents() – This function is used to write remote image data to a file.
// Remote image URL
$url = 'http://www.example.com/remote-image.png';
// Image path
$img = 'images/codexworld.png';
// Save image
file_put_contents($img, file_get_contents($url));
Save Image from URL using cURL
You can use cURL to save an image from URL using PHP. The following code snippet helps you to copy an image file from URL using cURL in PHP.
// Remote image URL
$url = 'http://www.example.com/remote-image.png';
// Image path
$img = 'images/codexworld.png';
// Save image
$ch = curl_init($url);
$fp = fopen($img, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_exec($ch);
curl_close($ch);
fclose($fp);
How to download file from URL using PHP
Building PHP applications will require file interaction a lot, one of them is download file from URL using PHP. This article will guide you the very basic methods of using PHP for downloading file from an URL.
To download file from URL using PHP
1. Using PHP file_get_contents() and file_put_contents() function:
This method can only be used if the web hosting allows the file_get_contents function to run. A lot of the web hosting turns off this function for security reasons , so you should check if the function is enabled before using this method. After successfully getting the file, file_put_contents will be used to actually save the file into a location.
If the message tells that the download is successful, the file will be store on the save path we defined.
2. Using PHP CURL and fopen()
The CURL method is more widely used, and we recommend you to use PHP CURL for downloading files from URLs instead of using file_get_contents function. CURL provides more compatibility, more controls over the downloading process and helps you to get familiar with using CURL in PHP, which will be crucial for many other network-related tasks in PHP. Also, working with fopen will be more convenient later with file interaction tasks. To download file from URL using PHP with CURL and fopen :
If no error displays after running the codes, the file will be stored at the defined $savePath location.
Final thoughts
The tutorial is now over. Hopefully it is helpful for you to understand the basic knowledge to download file from URL using PHP with file_get_contents and CURL. Thank you for reading!
You might also like
References
Download a file from the URL in PHP
In this post, I will try to explain to you how you can download any file by its URL with the help of PHP. You can do it in many ways but in this tutorial, I will explain to you a few tricks.
First Method
We will use file_get_contents() a built-in function of PHP. This function is similar to file() the only difference is file_get_contents() returns the file in a string. This function uses memory mapping techniques and it is a preferred way to read file content.
file_get_contents ( string $filename [, bool $use_include_path = FALSE [, resource $context [, int $offset = 0 [, int $maxlen ]]]] ) : string
The function returns the read data or FALSE on failure.
The above function will save the file on the same path where you run the script of PHP. If you want to download the file in your desired location then you need to set some headers. That is why I write a function given below that you can use to save file form URL into your local system.
The usage of the above function is given below.
Second Method.
In this method, I will show you how you can download a file with the helo of CURL another built-in function of PHP. If you use the below function you can save the file directly into your system by giving your desired location.
The usage of the above function is given below.
$urlPdf = 'http://www.africau.edu/images/default/sample.pdf'; dfCurl($urlPdf);
You can use any above function to download the file into your system or into your server.
Save Image From URL in PHP
- Save Image From URL Using file_get_contents() and file_put_contents() in PHP
- Save Image From URL Using cURL
- Save Image From URL Using the copy() Function in PHP
- Save Image From URL Using fread() and fwrite() in PHP
- Save a gzip Image in PHP
This article teaches five methods to save an image from a URL in PHP. These methods will use functions like file_put_contents() , copy() , fopen() , fread() , fwrite() , and gzdecode() .
Save Image From URL Using file_get_contents() and file_put_contents() in PHP
PHP file_get_contents() will read a file into a string while file_put_contents() can write that string to a file. Combining both functions will let you save an image from a URL.
First, you turn the image from the URL into a string using file_get_contents() , and then you use file_put_contents() to save this string into a file. The result will be your copy of the image from the URL.
In the following, we save an image from Imgur using file_get_contents() and file_put_contents() . Also, we rename the image to 01_computer_image.png , but you can use another name and a valid image extension.
php // The image is from Unsplash, and we've uploaded // it to Imgur for this article. $image_url = 'https://i.imgur.com/NFyDQTj.jpeg'; // Define the image location. Here, the location // is the saved_images folder. $image_location = 'saved_images/01_computer_image.png'; // Use file_put_contents to grab the image from // the URL and place it into the image location // with its new name. if (file_put_contents($image_location, file_get_contents($image_url))) echo "Image saved successfully"; > else echo "An error code, please check your code."; > ?>
Save Image From URL Using cURL
cURL is a command-line tool to transfer data using network protocols. Since the image exists in the URL on a server, you can start the cURL session that’ll save a copy to your computer.
In the following, we have a cURL session that’ll save an image from an Imgur URL.
php // Initiate a cURL request to the image, and // define its location. $curl_handler = curl_init('https://i.imgur.com/ZgpqSGm.jpeg'); $image_location = 'saved_images/02_apples.png'; // Open the file for writing in binary mode $open_image_in_binary = fopen($image_location, 'wb'); // Define where cURL should save the image. curl_setopt($curl_handler, CURLOPT_FILE, $open_image_in_binary); curl_setopt($curl_handler, CURLOPT_HEADER, 0); // Lets you use this script when there is // redirect on the server. curl_setopt($curl_handler, CURLOPT_FOLLOWLOCATION, true); // Auto detect encoding for the response | identity // deflation and gzip curl_setopt($curl_handler, CURLOPT_ENCODING, ''); // Execute the current cURL session. curl_exec($curl_handler); // Close the connection and curl_close($curl_handler); // Close the file pointer fclose($open_image_in_binary); // Confirm the new image exists in the saved_images // folder. if (file_exists($image_location)) echo "Image saved successfully"; > else echo "An error occurred. Please check your code"; > ?>
Save Image From URL Using the copy() Function in PHP
PHP copy() function can copy a resource from one location to another. To save the image from the URL, supply copy() with the URL and a new location.
To ensure you have the image, check for its existence using file_exists() .
php $image_url = 'https://i.imgur.com/CcicAAl.jpeg'; $image_location = 'saved_images/03_robot.png'; // Use the copy() function to copy the image from // its Imgur URL to a new file name in the // saved_images folder. $copy_image = copy($image_url, $image_location); // Confirm the new image exists in the saved_images // folder. if (file_exists($image_location)) echo "Image saved successfully"; > else echo "An error occurred. Please check your code"; > ?>
Save Image From URL Using fread() and fwrite() in PHP
PHP fread() will read an open file, while fwrite() will write to an open file. Knowing this, you can open the image URL using fopen() , read the image using fread() , then you can save it using fwrite() .
This sounds a bit more complex than it looks. That’s why we’ve created a function to simplify the process.
- Open the image in read-binary mode using fopen() .
- Open the image location in write-binary mode using fopen() .
- Read the image using fread() .
- Write the image to the image location.
- Close the handle to the opened image.
- Close the handle to the image location.
We’ve used this function to save a picture of a Mac.
php // Define a custom function to grab an image // from a URL using fopen and fread. function save_image_from_URL($source, $destination) $image_source = fopen($source, "rb"); $image_location = fopen($destination, "wb"); while ($read_file = fread($image_source, 8192)) fwrite($image_location, $read_file, 8192); > fclose($image_source); fclose($image_location); > // Set the image URL and its new destination on // your system $image_url = 'https://i.imgur.com/XGSex5B.jpeg'; $image_location = 'saved_images/04_Mac.png'; // Save the image to its new destination $save_image = save_image_from_URL($image_url, $image_location); // Confirm the new image exists in the saved_images // folder. if (file_exists($image_location)) echo "Image saved successfully"; > else echo "An error occurred. Please check your code"; > ?>
Save a gzip Image in PHP
If an image has gzip compression, the methods discussed in this article might not work. As a workaround, we’ve modified the first example that uses file_put_contents() and file_get_contents() .
This time, we grab the image headers and check for gzip encoding. If true, we decode the image before saving it; otherwise, we copy the image using PHP copy() function.
php // Set the image URL and its new location $image_url = 'https://i.imgur.com/PpJnfpL.jpeg'; $image_location = 'saved_images/05_Application_UI.png'; // Fetch all headers from the URL $image_headers = get_headers($image_url); // Check if content encoding is set $content_encoding = isset($image_headers['Content-Encoding']) ? $image_headers['Content-Encoding'] : null; // Set gzip decode flag $gzip_decode = ($content_encoding === 'gzip') ? true : false; // If the image is gzipped, decode it before // placing it in its folder. if ($gzip_decode) file_put_contents($image_location, gzdecode(file_get_contents($image_url))); > else copy($image_url, $image_location); > // Confirm the new image exists in the saved_images // folder. if (file_exists($image_location)) echo "Image saved successfully"; > else echo "An error occurred. Please check your code"; > ?>
Habdul Hazeez is a technical writer with amazing research skills. He can connect the dots, and make sense of data that are scattered across different media.