Skip to content

Commit 6ddcd3c

Browse files
author
XieBiao
committed
download file
1 parent f6c9436 commit 6ddcd3c

File tree

3 files changed

+54
-2
lines changed

3 files changed

+54
-2
lines changed

README.md

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ if ($ret) {
8585
```
8686

8787
```PHP
88-
//upload file
88+
//Upload file
8989
$postUrl = 'http://localhost/upload.php';//<?php var_dump($_FILES);
9090
$c = new Curl();
9191
$file1 = new \CURLFile('./olddriver.gif', 'image/gif', 'name1');
@@ -102,10 +102,30 @@ if ($ret) {
102102
}
103103
```
104104

105+
```PHP
106+
//Download file
107+
$fileUrl = 'http://localhost/test.gif';
108+
$options = [//The custom the curl options
109+
CURLOPT_TIMEOUT => 3600,//1 hour
110+
CURLOPT_CONNECTTIMEOUT => 10,
111+
];
112+
$c = new Curl($options);
113+
$c->makeGet($fileUrl);
114+
$ret = $c->exec();
115+
var_dump($ret);
116+
if ($ret) {
117+
//Success
118+
$targetFile = './a/b/c/test.gif';
119+
var_dump($c->responseToFile($targetFile));
120+
} else {
121+
//Fail
122+
var_dump($c->getError());
123+
}
124+
```
125+
105126
## TODO
106127

107128
* HTTP PUT/DELETE
108-
* Download file
109129
* Anything what you want
110130

111131
## License

examples/downloadfile.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
require '../vendor/autoload.php';
3+
4+
use Hhxsv5\PhpMultiCurl\Curl;
5+
6+
$fileUrl = 'http://localhost/test.gif';//<?php var_dump($_FILES);
7+
$options = [//The custom the curl options
8+
CURLOPT_TIMEOUT => 3600,//1 hour
9+
CURLOPT_CONNECTTIMEOUT => 10,
10+
];
11+
$c = new Curl($options);
12+
$c->makeGet($fileUrl);
13+
$ret = $c->exec();
14+
var_dump($ret);
15+
if ($ret) {
16+
//Success
17+
$targetFile = './a/b/c/test.gif';
18+
var_dump($c->responseToFile($targetFile));
19+
} else {
20+
//Fail
21+
var_dump($c->getError());
22+
}

src/Curl.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,16 @@ protected function hasError()
118118
return false;
119119
}
120120

121+
public function responseToFile($filename)
122+
{
123+
$response = $this->getResponse();
124+
$folder = dirname($filename);
125+
if (!file_exists($folder)) {
126+
mkdir($folder, 0777, true);
127+
}
128+
return file_put_contents($filename, $response);
129+
}
130+
121131
public function getResponse()
122132
{
123133
if ($this->response === null) {

0 commit comments

Comments
 (0)