在过去的两天里,我一直在寻找所有的地方并尝试一切,仍然无法得到任何工作.我觉得这应该是一件相对简单的事情.
我想要做的就是从URL下载远程文件到我服务器上的目录.
所以,例如,如果
$_url = http://www.freewarelovers.com/android/download/temp/1306495040_Number_Blink_1.1.1.apk
Run Code Online (Sandbox Code Playgroud)
和 $_dir = /www/downloads/
然后,当一切都说过和做过我想1306495040_Number_Blink_1.1.1.apk在/www/downloads/
我试过这个copy()功能,我试过了
file_put_contents("$_dir.$_file_name", file_get_contents($_url));
Run Code Online (Sandbox Code Playgroud)
并得到以下错误:
file_get_contents(): failed to open stream: HTTP request failed!
Spa*_*kup 15
这应该这样做:
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.
Run Code Online (Sandbox Code Playgroud)
Abd*_*him 14
$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);
Run Code Online (Sandbox Code Playgroud)
它使用卷曲
$ 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 );
Run Code Online (Sandbox Code Playgroud)
参考 http://www.tricksofit.com/2014/04/download-file-from-remote-server-in-php
小智 5
自 PHP 5.1.0 起,file_put_contents() 支持通过将流句柄作为 $data 参数传递来逐段写入:
无需使用 Curl
file_put_contents("Tmpfile.zip", fopen("http://someurl/file.zip", 'r'));
Run Code Online (Sandbox Code Playgroud)