[英]Download Remote File to Server with 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()
function,我试过
file_put_contents("$_dir.$_file_name", file_get_contents($_url));
并得到以下错误:
file_get_contents(): failed to open stream: HTTP request failed!
这应该这样做:
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.
$url = 'http://www.example.com/a-large-file.zip';
$path = '/path/to/a-large-file.zip';
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
file_put_contents($path, $data);
它使用 curl
$url 是文件 url
$path 是保存文件的位置和名称
我希望它有效
使用 curl 从远程服务器下载文件,如下所示。
$url = "http://path/toserver/filename";
$destination = "uploads/filename";
$fp = fopen ($destination, 'w+');
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_BINARYTRANSFER, true );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, false );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 10 );
curl_setopt( $ch, CURLOPT_FILE, $fp );
curl_exec( $ch );
curl_close( $ch );
fclose( $fp );
参考http://www.tricksofit.com/2014/04/download-file-from-remote-server-in-php
file_put_contents
需要一个文件名,而不是目录名。
从 PHP 5.1.0 开始,file_put_contents() 支持通过将流句柄作为 $data 参数传递来逐段写入:
无需使用 Curl
file_put_contents("Tmpfile.zip", fopen("http://someurl/file.zip", 'r'));
将其分为不同的阶段:
$raw = file_get_contents($_url);
... check if $raw has anything useful in it
file_put_contents($_dir, $raw);
... check if the file showed up
要么在 file_get_contents 中获取失败,要么在 file_put_contents 中写入失败,或者您正在下载的文件太大并且超出了 PHP 的默认 memory_limit。
通过验证...
首先验证文件是否存在:
function doesUrlExists($url) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if($code == 200){
$status = true;
}else{
$status = false;
}
curl_close($ch);
return $status;
}
然后放文件内容(用laravel存储类):
if(!doesUrlExists($url_file)) {
die('The remote file is not accessible. Please check the URL.');
}
Storage::disk('local')
->put($file_destintation, fopen($url_file, 'r'));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.