Do you want to be a good Web Developer?

Enter Your Email To Get Our Exclusive Tutorials Right In Your Email

Join TalkersCode Community And Recieve Exclusive Tutorials
On Web Development In Your Email

Upload Image From URL Using PHP

Friday, Jan 27, 2017

Tags:- PHP HTML


In this tutorial we will show you how to upload an image from url using PHP.You have to enter complete url of image which ends like file.jpg or any image extension and your image will be uploaded.You can use this method to download any kind of file and you can also create big application for file downloading.You may also like upload image to database and server using PHP.


See Demo
Upload Image From URL Using PHP


To Upload Image From URL It Takes Only Three Steps:-

  1. Make a HTML file and define markup
  2. Make a PHP file to upload image from url
  3. Make a CSS file and define styling


Step 1.Make a HTML file and define markup

We make a HTML file and save it with a name upload.html

<html>
<head>
<link href="upload_style.css" type="text/css" rel="stylesheet"/>
</head>
<body>
<div id="wrapper">

<div id="text_div">
 <form method="post" action="upload_image.php">
  <input type="text" name="img_url" placeholder="Enter Image URL">>
  <input type="submit" name="get_image" value="Submit">
 </form>
</div>

</div>
</body>
</html>

In this step we create a form to upload image from url entered by user and send it to 'upload_image.php' page for upload we also add css file which we were going to create in next step.You may also like upload image without page refresh.



Step 2.Make a PHP file to upload image from url

We make a PHP file and save it with a name upload_image.php

<?php
if(isset($_POST['get_image']))
{
 $url=$_POST['img_url'];
 $data = file_get_contents($url);
 $new = 'images/new_image.jpg';
 file_put_contents($new, $data);
 echo "<img src='images/new_image.jpg'>";
}
?>

In this step we get the image url and get image content using file_get_contents() function and then specify upload folder and image name in which we want to save image and then we use file_put_contents() to save image in upload folder.You may also like upload multiple images with preview.



Step 3.Make a CSS file and define styling

We make a CSS file and save it with a name upload_style.css

body
{
 margin:0 auto;
 padding:0px;
 text-align:center;
 width:100%;
 font-family: "Myriad Pro","Helvetica Neue",Helvetica,Arial,Sans-Serif;
 background-color:#34495E;
}
#wrapper
{
 margin:0 auto;
 padding:0px;
 text-align:center;
 width:995px;
}
#wrapper h1
{
 margin-top:50px;
 font-size:45px;
 color:#FDF2E9;
}
#wrapper h1 p
{
 font-size:18px;
}
#text_div input[type="text"]
{
 width:300px;
 height:35px;
 padding:10px;
}
#text_div input[type="submit"]
{
 width:100px;
 height:35px;
 margin-top:5px;
 background:none;
 border:1px solid white;
 color:white;
 font-weight:bold;
}
#text_div img
{
 width:200px;
}

That's all, this is how to upload image from URL using PHP and HTML.You can customize this code further as per your requirement. And please feel free to give comments on this tutorial.

Join Us With Our 21000+ Subscribers And Get Our Latest Tutorials Update Via Email