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.
index.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
<?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> |