使用 PHP 下载远程文件到服务器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6348602/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Download Remote File to Server with PHP
提问by Corey
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.
在过去的两天里,我一直在四处寻找并尝试了所有方法,但仍然无法正常工作。我觉得这应该是一件比较简单的事情。
All I want to do is download a remote file from a URL to a directory on my server.
我想要做的就是从一个 URL 下载一个远程文件到我服务器上的一个目录。
So, for example, if
所以,例如,如果
$_url = http://www.freewarelovers.com/android/download/temp/1306495040_Number_Blink_1.1.1.apk
and $_dir = /www/downloads/
和 $_dir = /www/downloads/
Then when all is said and done I want 1306495040_Number_Blink_1.1.1.apk
in /www/downloads/
然后当一切都说完了,我想1306495040_Number_Blink_1.1.1.apk
进去/www/downloads/
I've tried the copy()
function, I've tried
我试过这个copy()
功能,我试过了
file_put_contents("$_dir.$_file_name", file_get_contents($_url));
and get the following error:
并得到以下错误:
file_get_contents(): failed to open stream: HTTP request failed!
file_get_contents(): failed to open stream: HTTP request failed!
回答by Sparkup
This should do it :
这应该这样做:
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.
回答by Abdalla Mohamed Aly Ibrahim
$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);
it uses curl
它使用卷曲
$url is the file url
$url 是文件 url
$path is where and the name to save the file
$path 是保存文件的位置和名称
i hope it works
我希望它有效
回答by Harish Singh
Use curl to download file from remote server like below.
使用 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 );
reference http://www.tricksofit.com/2014/04/download-file-from-remote-server-in-php
参考 http://www.tricksofit.com/2014/04/download-file-from-remote-server-in-php
回答by Kuldeep
Since PHP 5.1.0, file_put_contents() supports writing piece-by-piece by passing a stream-handle as the $data parameter:
No need to use Curl
自 PHP 5.1.0 起,file_put_contents() 支持通过将流句柄作为 $data 参数传递来逐段写入:
无需使用 Curl
file_put_contents("Tmpfile.zip", fopen("http://someurl/file.zip", 'r'));
回答by ceejayoz
file_put_contents
expects a filename, not a directory name.
file_put_contents
需要文件名,而不是目录名。
回答by Marc B
Split it into different stages:
把它分成不同的阶段:
$raw = file_get_contents($_url);
... check if $raw has anything useful in it
file_put_contents($_dir, $raw);
... check if the file showed up
Either the fetch is failing in file_get_contents, or the write is failing in file_put_contents, or the file you're downloading is too large and exceeds your PHP's default memory_limit.
要么在file_get_contents 中获取失败,要么在file_put_contents 中写入失败,或者您下载的文件太大并且超出了PHP 的默认memory_limit。
回答by Nev Stokes
回答by JuliSmz
With validations...
通过验证...
Validate if file exists first:
首先验证文件是否存在:
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;
}
And then put file content (with laravel storage class):
然后放文件内容(使用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'));