簡體   English   中英

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網址重定向到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相同的代碼適用於其他隨機網址,例如www.example.com/download.php?id=2332 這里的具體問題是什么?

我已經測試了您的代碼,但也無法下載文件,因此,我使用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/具有寫權限。


小費:

將來,如果您需要對URL進行編碼/解碼,請使用urlencodeurldecode代替str_replace

這個連結

已經重定向到第二個鏈接。 所以它已經在工作了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM