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/
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)
Obrigado pelo comentário e dica.
Agurchand
Thanks thanks a lot….:-)
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
thanx alot it working fine
Thanks a lot this help me. at first i used fopen, but this works better. you can add option to create name and image extension
Great script! 🙂 Thank you very much
thanks a lot.. you taught me this today which i was searching since long.. Thank you!!!!!!!!!!!!!!!!
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,
Hi,
You may need to check your upload size limit with your hosting package.
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 ?
hi, please have a look at this tutorial https://theonlytutorials.com/php-jquery-ajax-image-upload-script-download-and-use-it/
yes but from url not from my pc
i can’t upload .mp3, direct link of mp3 upload in your demo link but it is not working in my link. only image upload. how i can upload mp3 and video from url. any other scripts?
this is my link : https://www.bigdownloadzone.com/upload.php
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”)
hey, what if i didn’t have the html form?
thanks
thanks for such a innovative solution
Hey, can you pls give me same script for Uploading Audio File(Mp3,M4a), and Video File (Mp4,mkv,avi). Pls Write a tutorial for this and notify me
You missed a parentheses – ( – after the file_put_contents function. Great tutorial though.
Very nice…
Thanks.
after adding or $ext == “mp4” in condition I still cant upload mp4
I love reading a post that will make people think.
Also, many thanks for allowing me to comment!
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