Remote upload in PHP

shaymiller

Regular Member
Joined
Dec 3, 2016
Messages
456
Reaction score
45
I have list of mp3 URLs. I want to upload those to my directory online. I tried my code with images, it works fine. as soon as I enter mp3 url, I upload mpe file with 0 bytes and error log shows "PHP Warning: file_get_contents(https://wwww.4864.mp3): failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error'

Code:
<?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 == "mp3" 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>



I am using wordpress plugin

I want stand alone outside of wordpress to able to run cron-job and auto run it.
 
Instead of using file_get_contents, use curl to download files from a URL. Curl is way more efficient than file_get_contents

The file output that you get from curl can be used in file_put_contents

Or fopen is also another option that should work.

PHP:
$upload = file_put_contents( "uploads/$name", fopen($url, 'r') );


PHP: fopen
 
Instead of using file_get_contents, use curl to download files from a URL. Curl is way more efficient than file_get_contents

The file output that you get from curl can be used in file_put_contents

Or fopen is also another option that should work.

PHP:
$upload = file_put_contents( "uploads/$name", fopen($url, 'r') );


PHP: fopen


Thanks, I tried that code, and getting same error

PHP Warning: fopen(https://funksyou.com/fileDownload/Songs/128/24222.mp3): failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error
 
Thanks, I tried that code, and getting same error

PHP Warning: fopen(https://funksyou.com/fileDownload/Songs/128/24222.mp3): failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error

Curl is your savior.

PHP:
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_REFERER, 'https://google.com/');
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:7.0.1) Gecko/20100101 Firefox/7.0.1");
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);

$res = curl_exec($curl);
curl_close($curl);

// you can also check for status code to verify if the request was successful => curl_getinfo($curl, CURLINFO_HTTP_CODE)

$upload = file_put_contents("uploads/$name", $res);
 
Curl is your savior.

PHP:
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_REFERER, 'https://google.com/');
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:7.0.1) Gecko/20100101 Firefox/7.0.1");
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);

$res = curl_exec($curl);
curl_close($curl);

// you can also check for status code to verify if the request was successful => curl_getinfo($curl, CURLINFO_HTTP_CODE)

$upload = file_put_contents("uploads/$name", $res);


THANK YOU it worked like a charm!!!

you can get it work using cron job with wp-cron.php
it will be the same


I couldn't find a way to do that. Its a plugin, so that means, I would need to be logged into admin account to able to run it. Then it has a form to input URL to grab those mp3 files. Basically what I did, I creasted an php file, which displays 1000+ mp3 files. when that plugin goes to that page, it downloads those mp3 files the way I want. As you can see, its manually. I wanted to auto run it Now that above code works, I can use cron job to automate. Only reason I was using WP, thats the only way I found to do that I wanted.
 
Curl is your savior.

PHP:
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_REFERER, 'https://google.com/');
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:7.0.1) Gecko/20100101 Firefox/7.0.1");
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);

$res = curl_exec($curl);
curl_close($curl);

// you can also check for status code to verify if the request was successful => curl_getinfo($curl, CURLINFO_HTTP_CODE)

$upload = file_put_contents("uploads/$name", $res);


I re-created the code, so on reload, it grabs URL from mysql and uploads to my directory perfectly. When I created a cron-job in my cPanel, it doesn't anything? I have a log every time it runs the cron job, I can see the URL query result, but no file in my directory?



@hideath Can you share that post? I searched that user profile & didn't see any post related to cron-job keywords


Thanks
 
The answer to using file_get_contents on a url is to enable allow_url_fopen.
 
It’s a PHP directive you can set by adding
Code:
allow_url_fopen=on
in your php.ini file. The CURL solution works too but for future readers setting this directive will allow you to use fopen on urls. It’s off by default because opening remote urls as if they were files can be a security risk.
 
I re-created the code, so on reload, it grabs URL from mysql and uploads to my directory perfectly. When I created a cron-job in my cPanel, it doesn't anything? I have a log every time it runs the cron job, I can see the URL query result, but no file in my directory?



@hideath Can you share that post? I searched that user profile & didn't see any post related to cron-job keywords


Thanks

Does it work when you refresh manually?
 
Does it work when you refresh manually?


yes
It’s a PHP directive you can set by adding
Code:
allow_url_fopen=on
in your php.ini file. The CURL solution works too but for future readers setting this directive will allow you to use fopen on urls. It’s off by default because opening remote urls as if they were files can be a security risk.


my allow_url_fopen is on. I checked with phpinfo()
 
yes



my allow_url_fopen is on. I checked with phpinfo()

It could be an issue with your cron job. Try an external website and see if it works, it's called cron-job with .org domain extension.
 
It could be an issue with your cron job. Try an external website and see if it works, it's called cron-job with .org domain extension.


Wow, thats a great service. is this totally free. If I plan to run every minute & forever? so far its working
 
Wow, thats a great service. is this totally free. If I plan to run every minute & forever? so far its working

Yes mate, it's free. Although I do recommend using your cpanel's cron job. Are you configuring it right?
 
FOpen is clunky for a number of reasons, CURL is the best way to do get the data and most modern web hosts should have that library installed by default.

Some servers also don't allow blank user agents, so remember to spoof the user agent when making calls to servers like that.
 
Yes mate, it's free. Although I do recommend using your cpanel's cron job. Are you configuring it right?

/usr/local/bin/php /home/username/public_html/news/load.php >> /home/username/public_html/news/load.log




FOpen is clunky for a number of reasons, CURL is the best way to do get the data and most modern web hosts should have that library installed by default.

Some servers also don't allow blank user agents, so remember to spoof the user agent when making calls to servers like that.

so I should do something like load.php?some_variable=same_url
 
Last edited:
/usr/local/bin/php /home/username/public_html/news/load.php >> /home/username/public_html/news/load.log






so I should do something like load.php?some_variable=same_url

You can try these since I don't know your php path:

/usr/bin/php /home/username/public_html/news/load.php >> /home/username/public_html/news/load.log

php -q /home/username/public_html/news/load.php >> /home/username/public_html/news/load.log
 
Back
Top