How to Download Image From URL in PHP
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.
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.
Related searches
- How to send email from PHP without SMTP server
- PHP Basic Interview Questions and Answers in Hindi
- difference between echo, print, and print_r in PHP
- form validation in php and mysql | Vkprogramming
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>
Related searches
- How to send email from PHP without SMTP server
- PHP Basic Interview Questions and Answers in Hindi
- difference between echo, print, and print_r in PHP
- form validation in php and mysql | Vkprogramming
How to Save Image from URL using PHP
Save Image from URL using PHP
The following code snippet helps you to copy an image file from remote URL and save in a folder using PHP.
- file_get_contents() – This function is used to read the image file from URL and return the content as a string.
- file_put_contents() – This function is used to write remote image data to a file.
// Remote image URL $url = 'http://www.example.com/remote-image.png'; // Image path $img = 'images/codexworld.png'; // Save image file_put_contents($img, file_get_contents($url));
Save Image from URL using cURL
You can use cURL to save an image from URL using PHP. The following code snippet helps you to copy an image file from URL using cURL in PHP.
// Remote image URL $url = 'http://www.example.com/remote-image.png'; // Image path $img = 'images/codexworld.png'; // Save image $ch = curl_init($url); $fp = fopen($img, 'wb'); curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_HEADER, 0); curl_exec($ch); curl_close($ch); fclose($fp);
Related searches
- How to send email from PHP without SMTP server
- PHP Basic Interview Questions and Answers in Hindi
- difference between echo, print, and print_r in PHP
- form validation in php and mysql | Vkprogramming
- What is the difference between echo and print in php
- PHP Developer Jobs and Vacancies in india 2022
- Entry Level Php Developer Salary in the US 2022