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 Generate PDF using HTML Form in PHP using MPDF - Coding Shiksha

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 Upload & Download Basic Video to MySQL Database in PHP - Coding Shiksha

 

index.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">
  <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>
  </form>
  <br>
  <div id="image">
  <?php
  if(isset($filename) && isset($ext))
  {
  echo '<img width="300px" height="300px" src='. $filename . '.' . $ext . '>';
  }
  ?>
  </div>
</div>
</body>
<html>

Leave a Reply

Close Menu