Very Simple PHP Script to Upload Image or file from an URL!

A very simple PHP Script i have written here. You can see the demo & download the script at the bottom of the post!

How difficult if you want to uplaod a Photo / Image or a PDF from an URL. Do you thing this is so difficult?.

Have you ever used file_get_contents and file_put_contents?. If you don’t you got a chance now to do it!.

If you are a PHP Developer don’t worry!. Here is the simple script will do the trick for you and you can easily understand by reading the comments in the script itself!

See the snippet below (please make sure your folder is writable (permission: 775 or 777)):

// this three lines of code will upload an image from any url to the your folder!

$url = "https://theonlytutorials.com/images/logo.png";
$name = basename($url);
file_put_contents("uploads/$name", file_get_contents($url);

For your convenience I have written a small program where you can copy paste or download and use it in your own projects!

<?php
//blog.theonlytutorials.com
//author: agurchand

if($_POST){
//get the url
$url = $_POST['url'];

//add time to the current filename
$name = basename($url);
list($txt, $ext) = explode(".", $name);
$name = $txt.time();
$name = $name.".".$ext;

//check if the files are only image / document
if($ext == "jpg" or $ext == "png" or $ext == "gif" or $ext == "doc" or $ext == "docx" or $ext == "pdf"){
//here is the actual code to get the file from the url and save it to the uploads folder
//get the file from the url using file_get_contents and put it into the folder using file_put_contents
$upload = file_put_contents("uploads/$name",file_get_contents($url));
//check success
if($upload)  echo "Success: <a href='uploads/".$name."' target='_blank'>Check Uploaded</a>"; else "please check your folder permission";
}else{
echo "Please upload only image/document files";
}
}
?>

<html>
<head><title>Theonlytutorials - Simple File Upload from URL Script!</title></head>
<body>
<h3>Theonlytutorials.com - Very Simple File Upload from URL Script!</h3>
Paste the url and hit enter!
	<form action="" method="post">
		Your URL: <input type="text" name="url" />
	</form>
</body>
</html>

 

Secure your ‘uploads’ folder. Learn how to here: https://theonlytutorials.com/learn-prevent-executing-php-file-folder-directory/

Download

25 thoughts on “Very Simple PHP Script to Upload Image or file from an URL!

  1. Foi o melhor que já vi.

    Muito obrigado!

    Quer uma dica? Faltou apenas um botão.

    Mas mesmo assim foi muito boa essa sua dica.

    Valeu mesmo!

    de: jsf017bra

    (Brasil)

  2. Hello! Very good script. If I want to upload a link to request a login and password, I would add the login and password already in the script? Thank you

  3. thanks a lot.. you taught me this today which i was searching since long.. Thank you!!!!!!!!!!!!!!!!

  4. Hi there,
    I do really appreciate your script and I tested on my server.
    But main error is when i upload little bit huge files, it gives me 500 gate-way time out error.
    What i can do for that?
    Please reply to me.

    Thanks so much.

    With Regards,

  5. perfect.thank you for something that I was looking for a very long time.
    would be possible for you to modify the script with upload button and the way so it will perform upload without refreshing the page and will produce ready link to uploaded file ?

    1. because it is limited to images, see the if condition in line no.16. Add mp3 also in the if condition, it would be like this
      if($ext == “mp3” $ext == “jpg” or $ext == “png” or $ext == “gif” or $ext == “doc” or $ext == “docx” or $ext == “pdf”)

  6. Hi,

    Great Tutorial !! But What if the source file is from localhost and going to a certain url server? is this possible? Very much appreciated if you could just give a sample script in PHP. Thanks

Leave a Reply

Theme: Overlay by Kaira
Agurchand