Php download image from url to server

From Copy images from url to server, delete all images after

function getimg($url) {         
    $headers[] = 'Accept: image/gif, image/x-bitmap, image/jpeg, image/pjpeg';              
    $headers[] = 'Connection: Keep-Alive';         
    $headers[] = 'Content-type: application/x-www-form-urlencoded;charset=UTF-8';         
    $user_agent = 'php';         
    $process = curl_init($url);         
    curl_setopt($process, CURLOPT_HTTPHEADER, $headers);         
    curl_setopt($process, CURLOPT_HEADER, 0);         
    curl_setopt($process, CURLOPT_USERAGENT, $user_agent); //check here         
    curl_setopt($process, CURLOPT_TIMEOUT, 30);         
    curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);         
    curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);         
    $return = curl_exec($process);         
    curl_close($process);         
    return $return;     
} 

$imgurl = 'http://www.foodtest.ru/images/big_img/sausage_3.jpg'; 
$imagename= basename($imgurl);
if(file_exists('./tmp/'.$imagename)){continue;} 
$image = getimg($imgurl); 
file_put_contents('tmp/'.$imagename,$image);       

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.

Example:

File downloaded!

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.

Approach:

  • 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.

Example:

function file_get_contents_curl($url) {

    $ch = curl_init();

    curl_setopt($ch, CURLOPT_HEADER, 0);

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

    curl_setopt($ch, CURLOPT_URL, $url);

    $data = curl_exec($ch);

    curl_close($ch);

    return $data;

}

$data = file_get_contents_curl(

$fp = 'logo-1.png';

file_put_contents( $fp, $data );

echo "File downloaded!"

?>

Output:

File downloaded!

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.

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.


How can I download image from URL in PHP?

Save Image from URL 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..

How do I download an image from a URL?

Click on the Download Image from URL button, the field will appear on the right. Enter the full web address of the image. Click on the arrow to the right of the field and select the Force Check checkbox. Then click the Save button.

How can I download image from PHP?

You can force images or other kind of files to download directly to the user's hard drive using the PHP readfile() function. Here we're going to create a simple image gallery that allows users to download the image files from the browser with a single mouse click. Let's create a file named "image-gallery.