Sometimes, need to download an image from a particular URL and use it into the project. It’s easy to go to the page and use right click button and save the image. But what if you wanted to do it programmatically? The reasons may vary person to person, developer to developer. If set of hundreds of image URLs given and somehow want to save them into the machine, or need to use this concept into the projects. Then definitely not going to download each one of those files manually.
There are two different approaches to download image from url which are listed below:
- Using basic file handling.
- Using an HTTP library called cURL.
Both of these approaches come with their own set of merits and demerits.
Using Basic File Handling: This is the fundamental and easiest way to accomplish the task. Just like any other file, start with the creation of an empty file and open it in “write” mode. After that, fetch the content from source URL and paste it into this file. And it is as simple as it sounds.
From the script, you can figure out on your own about what it does.
- Declaring two variables named as $url and $img, representing the source URL and destination file respectively.
- Use file_put_contents() function to write a string to a file that takes two arguments. One is the file name (or path) and the other is the content for that file.
- Use file_get_contents() function to read a file into a string.
Note: It save the image to the server with given name logo.png.
Now the only problem with this method is that it requires allow_url_fopen configuration to be set, which is set to 1 by default. But sometimes, project requirements don’t allow to have this option. This may be because of some preventive security measures or just a design principle. In such cases, there is another method to save image.
Using HTTP library, cURL: Strictly speaking, cURL is not just an HTTP library. It has got several other data transferring protocols as well. As our image is on an HTTP server, we will limit ourselves to this small section of this library.
cURL allows to make HTTP requests in PHP. Start by initializing an instance of it and setting up some of the necessary options for the request, including the URL itself. Then execute this query which returns the content of the file. After that, the rest of the procedure is the same. As soon as we get the data, put it into a file and save it.
- In this script, we defined a function file_get_contents_curl to replicate the behaviour of file_get_contents from the previously mentioned technique.
- Inside this function, we have initialised an instance of cURL using curl_init function in order to use it for fetching the data.
- After that, some options need to be set using curl_setopt so that this particular example can work. This function takes three arguments
- An instance of cURL
- The corresponding option that need to be set
- And the value to which option is set
In this example, the following options are set:
- CURLOPT_HEADER, which is to ensure whether you need to receive the headers or not;
- CURLOPT_RETURNTRANSFER which transfers data as the return value of curl_exec function rather than outputting it directly.
- There is this another option CURLOPT_URL that sets the URL for the request.
- After that, we fetch the data from curl_exec and return it from the parent function.
- This data is then written on to the file on your machine using file_put_contents.
This method provides a bit of flexibility while fetching the content from the internet. As mentioned earlier, it is not just limited to HTTP but can be used in many other circumstances as well. It allows to configure the transfer in whatever way you want. For example, file_get_contents uses a simple GET request to fetch the data, but with cURL, can use GET, POST, PUT and other methods as well.
- Saving a file in Git
- Saving What Saves Our Passwords – Two-Factor Authentication
- Convert an image into grayscale image using HTML/CSS
- CSS | Image Gallery
- CSS | Image Sprites
- HTML | DOM Image Object
- Resize image proportionally with CSS
- CSS | border-image Property
- CSS | background-image Property
- How to get the width and height of an image ?
- jQuery | :image Selector
- HTML | Mapping Image
- Set the size of background image using CSS ?
- Drop shadow for PNG image using CSS
- How to change color of PNG image using CSS?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.