PHP copy() 不适用于重定向到文件的随机 URL
PHP copy() doesn't work for random URLS which redirect to files
正在尝试从远程 url 复制 () .MP3 文件,但总是失败。
$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 重定向到 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
相同的代码适用于其他随机 url,如 www.example.com/download.php?id=2332
。这里的具体问题是什么或以任何其他方式完成这项工作?
已经重定向到第二个 link。所以它已经在工作了。
我已经测试了你的代码,但我也无法下载文件,然后,我使用了 curl,它按预期工作:
$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);
注意:
确保 /home2/muser/tmp/
具有写入权限。
提示:
以后如果需要encode/decode一个url,用urlencode or urldecode代替str_replace
正在尝试从远程 url 复制 () .MP3 文件,但总是失败。
$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 重定向到 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
相同的代码适用于其他随机 url,如 www.example.com/download.php?id=2332
。这里的具体问题是什么或以任何其他方式完成这项工作?
已经重定向到第二个 link。所以它已经在工作了。
我已经测试了你的代码,但我也无法下载文件,然后,我使用了 curl,它按预期工作:
$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);
注意:
确保 /home2/muser/tmp/
具有写入权限。
提示:
以后如果需要encode/decode一个url,用urlencode or urldecode代替str_replace