All TalkersCode Topics

Follow TalkersCode On Social Media - 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

<link href="upload_style.css" type="text/css" rel="stylesheet"/>
<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">


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

 $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

 margin:0 auto;
 font-family: "Myriad Pro","Helvetica Neue",Helvetica,Arial,Sans-Serif;
 margin:0 auto;
#wrapper h1
#wrapper h1 p
#text_div input[type="text"]
#text_div input[type="submit"]
 border:1px solid white;
#text_div img

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 🡪