Download Remote File to Server with PHP(使用 PHP 下载远程文件到服务器)
问题描述
过去两天我一直在四处寻找并尝试了所有方法,但仍然无法正常工作.我觉得这应该是一件比较简单的事情.
I've been looking all over the place for the last two days and trying everything and still can't get anything to work. I feel like this should be a relatively simple thing to do.
我想要做的就是将远程文件从 URL 下载到我服务器上的目录.
All I want to do is download a remote file from a URL to a directory on my server.
例如,如果
$_url = http://www.freewarelovers.com/android/download/temp/1306495040_Number_Blink_1.1.1.apk
和 $_dir =/www/downloads/
然后当一切都说完了我想要1306495040_Number_Blink_1.1.1.apk
在/www/downloads/
Then when all is said and done I want 1306495040_Number_Blink_1.1.1.apk
in /www/downloads/
我试过 copy()
函数,我试过
I've tried the copy()
function, I've tried
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.
这篇关于使用 PHP 下载远程文件到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 PHP 下载远程文件到服务器


基础教程推荐
- HTTP 与 FTP 上传 2021-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- 使用 PDO 转义列名 2021-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01