微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用PHP将远程文件下载到服务器

在过去的两天里,我一直在寻找所有的地方并尝试一切,仍然无法得到任何工作.我觉得这应该是一件相对简单的事情.

我想要做的就是从URL下载远程文件到我服务器上的目录.

所以,例如,如果

$_url = http://www.freewarelovers.com/android/download/temp/1306495040_Number_Blink_1.1.1.apk

和$_dir = / www / downloads /

然后,当所有的说完成后,我想要/ www / downloads /中的1306495040_Number_Blink_1.1.1.apk

我试过了copy()函数,我试过了

file_put_contents("$_dir.$_file_name",file_get_contents($_url));

并得到以下错误

file_get_contents():无法打开流:HTTP请求失败!

这应该这样做:
set_time_limit(0);

$url = 'http://www.freewarelovers.com/android/download/temp/1306495040_Number_Blink_1.1.1.apk';
$file = fopen(dirname(__FILE__) . '/downloads/a.apk','w+');

$curl = curl_init();

// Update as of PHP 5.4 array() can be written []
curl_setopt_array($curl,[
    CURLOPT_URL            => $url,//  CURLOPT_BINARYTRANSFER => 1,--- No effect from PHP 5.1.3
    CURLOPT_RETURNTRANSFER => 1,CURLOPT_FILE           => $file,CURLOPT_TIMEOUT        => 50,CURLOPT_USERAGENT      => 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)'
]);

$response = curl_exec($curl);

if($response === false) {
    // Update as of PHP 5.3 use of Namespaces Exception() becomes \Exception()
    throw new \Exception('Curl error: ' . curl_error($curl));
}

$response; // Do something with the response.

原文地址:https://www.jb51.cc/php/133872.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐