Download Image from URL in PHP

In this post, I share simple code to download and view images from URLs in PHP.

HTML

Table of Contents

Create a simple HTML form


<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<title>Download Image from URL in PHP</title>
</head>
<body>
<div class="container">
    <div class="row">
        <div class="col-md-6">
            <h1>Enter Image URL</h1>
            <form action="" method="post">
                <input name="url" type="text" class="form-control" placeholder="Enter URL"><br>
                <button class="btn btn-success">Download Image</button>
            </form>
        </div>
        <div class="col-md-6">
           
        </div>
    </div>
</div>
</body>
<html>

PHP

We have used file_get_contents() method here. file_get_contents() is the preferred way to read the contents of a file into a string. It will use memory mapping techniques if supported by your OS to enhance performance.

<?php
    if(isset($_POST['url']))
        {
        $url = $_POST['url'];
        $pathinfo = pathinfo($url); // To get the filename and extension
        $ext = $pathinfo['extension']; 
        $filename = 'images/'.$pathinfo['filename']; 
        $img = @file_get_contents($url,true); // get the image from the url
        file_put_contents($filename.'.'.$ext, $img); // create a file and feed the image
    }
?>

And View Downloaded image

<?php
    if(isset($filename) && isset($ext))
    {
        echo '<img width="300px" height="300px" src='. $filename . '.' . $ext . '>';
    }
?>

Index.php

Full code for Download Image from URL in PHP

<?php
    if(isset($_POST['url']))
        {
        $url = $_POST['url'];
        $pathinfo = pathinfo($url); // To get the filename and extension
        $ext = $pathinfo['extension']; 
        $filename = 'images/'.$pathinfo['filename']; 
        $img = @file_get_contents($url,true); // get the image from the url
        file_put_contents($filename.'.'.$ext, $img); // create a file and feed the image
    }
?>

<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<title>Download Image from URL</title>
</head>
<body>
<div class="container">
    <div class="row">
        <div class="col-md-6">
            <h1>Enter Image URL</h1>
            <form action="" method="post">
                <input name="url" type="text" class="form-control" placeholder="Enter URL"><br>
                <button class="btn btn-success">Download Image</button>
            </form>
        </div>
        <div class="col-md-6">
            <div id="image">
                <?php
                    if(isset($filename) && isset($ext))
                    {
                        echo '<img width="300px" height="300px" src='. $filename . '.' . $ext . '>';
                    }
                ?>
            </div>
        </div>
    </div>
</div>
</body>
<html>

I hope it will help you. Thank you!