How to Download Image From URL in PHP

  • Post author:
  • Post category:PHP
  • Post comments:0 Comments

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




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.
See also  PHP 7 Script to Scrape or Extract Title and Meta Description of Website URL Full Project For Beginners




$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