PHP copy()不适用于重定向到文件的随机URL
trying to copy() .MP3 file from remote url but it always fails.
$link = str_replace(' ','%20','http://mp3hungama.com/music/download.php?song_id=80522');
if (!copy($link,'/home2/muser/tmp/newname.mp3')) {
echo 'copy failed !';
}
$link url redirects to http://mp3hungama.com/music/audio//Indian%20Movies/Indian%20Movies%20Hindi%20Mp3%20Songs/Singh%20Is%20Bling%20(2015)/songs/Cinema%20Dekhe%20Mamma%20@%20Mp3HunGama.Com.mp3
same code works for others random urls like www.example.com/download.php?id=2332
. what's the specifically problem here or any other way to do this job ?
尝试从远程网址复制().MP3文件但总是失败。 p>
$ link = str_replace('','%20','http://mp3hungama.com/music/download.php?song_id = 80522');
if(!copy($ link,' /home2/muser/tmp/newname.mp3')){
echo'copy failed!';
}
code> pre>
$ link url重定向到 http://mp3hungama.com/music/audio//Indian%20Movies/Indian%20Movies%20Hindi%20Mp3%20Songs/Singh%20Is%20Bling%20(2015)/歌曲/电影%20Dekhe%20Mamma%20 @% 20Mp3HunGama.Com.mp3 code>
same代码适用于其他随机网址,例如 www.example.com/download.php?id=2332 code>。 这里有什么具体问题或者其他任何方式来完成这项工作? p>
div>
I've tested your code and I also couldn't download the file, then, I've used curl an it work as expected:
$local_file = "/home2/muser/tmp/newname.mp3";//This is the file where we save the information
$remote_file = "http://mp3hungama.com/music/download.php?song_id=80522"; //Here is the file we are downloading
$ch = curl_init();
$fp = fopen ($local_file, 'w+');
$ch = curl_init($remote_file);
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_ENCODING, "");
curl_exec($ch);
curl_close($ch);
fclose($fp);
NOTE:
Make sure /home2/muser/tmp/
has write permissions.
TIP:
In the future, if you need to encode/decode a url, use urlencode or urldecode instead of str_replace