duandun2136 2015-10-09 17:47
浏览 34
已采纳

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 ?

  • 写回答

2条回答 默认 最新

  • douxian1923 2015-10-09 17:59
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看