All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Upload Image From URL Using PHP

Last Updated : Jul 1, 2023

IN - PHP HTML | Written & Updated By - Pragati

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.

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.

I hope this tutorial on upload image from URL helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪