How to download image in php

How to download image in php

Question: How to download image from url and save in server?

//Image path which we will download
$imageURL='http://domain.com/image.jpg';

//get extension of image
$ext = pathinfo($imageURL, PATHINFO_EXTENSION);

//Local path of image - where will we save the image
$downloadTo = fopen('import/image/file'.  rand(1, 9999).'.'.$ext, 'wb');

//Download and save image
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $imageURL);
curl_setopt($ch, CURLOPT_FILE, $downloadTo);
curl_setopt($ch, CURLOPT_HEADER, 0);
$output = curl_exec($ch);
curl_close($ch);

For http URL, http image will download.
For https URL, https image will download.

Question: How to get image extension from url?

$imageURL='http://domain.com/image.jpg';
$ext = pathinfo($imageURL, PATHINFO_EXTENSION); //.jpg

Question: Download image from URL and display in browser?

    //Image path which we will download
    $imageURL='http://domain.com/image.jpg';

    //get extension of image
    $ext = pathinfo($imageURL, PATHINFO_EXTENSION);

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $imageURL);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    $output = curl_exec($ch);
    curl_close($ch);
    switch( $ext ) {
        case "gif": $ctype="image/gif"; break;
        case "png": $ctype="image/png"; break;
        case "jpeg":
        case "jpg": $ctype="image/jpeg"; break;
        default:
    }
    header('Content-type: ' . $ctype);

For http URLhttp image will download.
For https URLhttps image will download.

Question: How to check if image is valid OR Not?

$imageURL='http://domain.com/image.jpg';
 list($width, $height, $type, $attr) = getimagesize($imageURL);
 if (!empty($width) && !empty($height)) {
    echo "Valid Image";
    }else{
    echo "In Valid Image";
    }

Question: Download image from external URL, instead of displaying in browser?

        $imageURL='https://s3.ap-south-1.amazonaws.com/cloud-front-s3/8_12.jpg';
        $imageURLArray = explode('/',$imageURL);
        $downloadFileName=$imageURLArray[count($imageURLArray)-1];
        
        //get extension of image
        $ext = pathinfo($imageURL, PATHINFO_EXTENSION);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $imageURL);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        $output = curl_exec($ch);
        curl_close($ch);
      
            
        header("Content-Type: application/octet-stream"); //
        // set it as an attachment and give a file name
        header('Content-Disposition: attachment; filename='.$downloadFileName);            
        echo $output;
        

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($imgfile_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($chCURLOPT_FILE$fp);
curl_setopt($chCURLOPT_HEADER0);
curl_exec($ch);
curl_close($ch);
fclose($fp);

How can I download JPG file in PHP?

download=true&g={$gallery}&i={$image}". The link takes a lonnnnnnnnng time, and then it brings up a dialog prompt asking you to Open or Save the file, but once you Save and try to open it, it says the file is corrupt and can't be opened.

How can I download image from API 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 can I download image from database in PHP?

download image from mysql using php.
//You can save this to test.php and call it with test.php? id=1 for example..
<? php..
//Database class with PDO (MySQL/MariaDB).
require_once("database.php"); //If you need this, write to [email protected] i'll send you the db class..
//Connect to database..
$database = new Database();.

How do I download a PHP file?

PHP enables you to download file easily using built-in readfile() function. The readfile() function reads a file and writes it to the output buffer..
<? ... .
header('Content-Type: application/octet-stream');.
header("Content-Transfer-Encoding: utf-8");.