There is a file with a bunch of links like "http://www.ex.ua/load/ * *", on which audio files are downloaded (if the link is scored in the browser or used with the wget command). I unsuccessfully try to make a script for uploading these files from the network (the $ links array contains links):
option 1:
foreach ($links as $key => $link) { $fs = fopen($link, 'rb'); $file = fopen('files/'.($key + 1), 'w'); if ($fs && $file) { echo($link.' is downloading.<br/>'); while (!feof($fs)) { fwrite($file, fread($fs, 4096)); } } fclose($file); fclose($fs); } option 2:
foreach ($links as $key => $link) { $c = curl_init($link); $file = fopen('files/'.($key + 1), 'wb'); curl_setopt($c, CURLOPT_FILE, $file); curl_setopt($c, CURLOPT_RETURNTRANSFER, 0); curl_setopt($c, CURLOPT_HEADER, true); curl_exec($c); curl_close($c); fclose($file); } option 3:
foreach ($links as $key => $link) { if (!copy($link, 'files/'.($key + 1))) { echo $link.' copy\'s error<br/>'; } } option 4:
foreach ($links as $key => $link) { $ch = curl_init($link); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_BINARYTRANSFER, true); curl_setopt($ch, CURLOPT_USERAGENT, 'Googlebot/2.1 ( http://www.googlebot.com/bot.html)'); $output = curl_exec($ch); $fh = fopen('files/'.($key + 1), 'w'); fwrite($fh, $output); fclose($fh); } option 5:
foreach ($links as $key => $link) { file_put_contents('files/'.($key + 1), file_get_contents($link)); } And no option does what is needed - does not download the required files. When using option 1, 3, 5, the resulting files contain the html / css code of the page that contains the link to the required file. When using options 2, 4, the resulting files contain
<html> <head><title>400 Bad Request</title></head> <body bgcolor="white"> <center><h1>400 Bad Request</h1></center> <hr><center>nginx/1.6.2</center> </body> </html> So I did not understand how to download files. Maybe someone will tell? :)
UPD.
Screenshots of the request / response when uploading a file by the browser:
Shl. It is impossible to put it in a convenient format (in the form of thumbnails / pictures), therefore I place links to screenshots.