Menu Close

Upload Image From URL in PHP

codeamend

Let’s see  how to upload image from the URL in PHP.

It is very simple to upload image from URL to server.

Here, you have to enter the complete URL including image name and clicking on upload button then image will upload to server.

Upload Image From URL using PHP.

Create a HTML form to submit an image URL

<!DOCTYPE html>
<html>
    <body>
        <form method="POST">
            <input type="text" name="image_url" placeholder="Enter image url">
            <input type="submit" name="upload_image" value="Upload">
        </form> 
    </body>
</html> 

PHP snippet to upload an image to server on the form submission.

<?php
if(isset($_POST['upload_image'])) {
    $src_image =   file_get_contents(filter_input(INPUT_POST, "image_url"));
    $new_info = "images/new.jpg";
    $status = file_put_contents($new_info, $src_image);
    if($status){
        echo '<img src="images/new.jpg">';   
    } else {
        echo "not uploaded";    
    }
}
?> 
Posted in PHP

You can also read...