How to Download Image From URL in PHP – Coding Shiksha

How to Download Image From URL in PHP – Coding Shiksha

Welcome Folks I am back with another blog post. In this post We will be making an application in which we will be Downloading Images From URL in PHP. The demo of the application is given below in this video as follows.


Demo of the Application




READ  How to Upload Files with PHP 7

Full Source Code of Application


For this Application we just require two things first of all create a new directory and in that directory make two things

  • Make a index.php file in which we will be having the form in which user can enter url of the image and then the download image button
  • Secondly just create the images folder in the same directory so that the uploaded images will be stored in this directory.
READ  How to Force a Image File to Download Automatically in PHP - Coding Shiksha




$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>
<link rel="stylesheet" href="">
<title>Download Image from URL</title>
<div class="container">
  <h1>Download Image from URL in PHP</h1>
  <form action="" method="post">
      <input name="url" type="text" class="form-control" placeholder="Enter URL of Image"><br>
      <button class="btn btn-success">Download Image</button>
  <div id="image">
  if(isset($filename) && isset($ext))
  echo '<img width="300px" height="300px" src='. $filename . '.' . $ext . '>';

Leave a Reply

Close Menu

Indian Desi Tools - Ultimate Tools Website

Demo Description

This will close in 20 seconds