Upload Image From URL in PHP

In this tutorial we will see how to upload image from URL in PHP.

It is very easy to upload image from url to server.

In this example, you have to pass complete image path including image name and clicking on upload button will upload image to server.

This is very simple explanation about upload image from URL to server in PHP.

Upload Image From URL in PHP

Read:

Ajax image upload with jQuery and PHP

Upload Multiple Images with jQuery and PHP

HTML
First create a HTML form to enter an image URL.

<form method="POST">
<input type="text" name="image_url" placeholder="Paste image url">
<input type="submit" name="upload_image" value="Upload">
</form> 

PHP
Write php snippet to upload image to server on form submission.

   <?php
     if(isset($_POST['upload_image'])) {
    //read a file from source
    $src_image =   file_get_contents(filter_input(INPUT_POST, "image_url"));

    //give new file name
    $new_info = "images/new_image.jpg";

    //write data to a file
    $status = file_put_contents($new_info, $src_image);

    if($status)
    {
    echo '<img src="images/new_image.jpg">';    
    } else
    {
    echo "not uploaded";    
    }
} ?>

You can also upload any doc file to server using above steps. This example demonstrate about how to upload an image file from URL to server in PHP.

Leave a Comment