-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathphp_curl_file.php
388 lines (320 loc) · 10.9 KB
/
php_curl_file.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
###form表单上传
最开始接触html上传文件是一个表单,然后一个input `<input name="file" type="file" id="file"/>` ,点击直接上传,服务器端通过`$_FILES` 接收处理,一个简单的demo:
```php
//upload.php
$uploaddir = './pic/';
$uploadfile = $uploaddir . basename($_FILES['file']['name']);
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
echo "上传成功";
} else {
echo "上传失败";
}
?>
<form action="upload.php" method="post" name="frmUpload" enctype="multipart/form-data">
<tr>
<td>Upload</td>
<td align="center">:</td>
<td><input name="file" type="file" id="file"/></td>
</tr>
<tr>
<td> </td>
<td align="center"> </td>
<td><input name="btnUpload" type="submit" value="上传" /></td>
</tr>
```
###base64上传
可以将图片文件转为base64编码,然后提交字符串的形式上传,服务器端只需要简单处理即可
```php
file_put_contents($file, $_POST['base64']);
```
###[curl上传](https://github.com/php-curl-class/php-curl-class)
[php文档](http://www.php.net/manual/zh/function.curl-setopt.php) 传递一个数组到CURLOPT_POSTFIELDS,cURL会把数据编码成 multipart/form-data,而然传递一个URL-encoded字符串时,数据会被编码成 application/x-www-form-urlencoded
```php
$ch = curl_init();
$data = ['name' => 'foo', 'file' => '@/home/test.png'];
curl_setopt($ch, CURLOPT_URL, 'http://localhost/curl/upload_file.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SAFE_UPLOAD, false);// php5.6改成 true
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_exec($ch);
//命令行测试下 curl -F "@/home/test.png" http://localhost/curl/upload_file.php
```
[PHP从5.5开始引入了新的CURLFile类用来指向文件](https://segmentfault.com/a/1190000000725185)
CURLFile类也可以详细定义MIME类型、文件名等可能出现在multipart/form-data数据中的附加信息。PHP推荐使用CURLFile替代旧的@语法
```php
if (class_exists('\CURLFile')) {
$field = array('fieldname' => new \CURLFile(realpath($filepath)));
} else {
$field = array('fieldname' => '@' . realpath($filepath));
}
```
###拼接字符模拟文件上传
```php
/** HttpRequest class, HTTP请求类,支持GET,POST,Multipart/form-data
* 代码来自osc
* Func:
* public setConfig 设置连接参数
* public setFormdata 设置表单数据
* public setFiledata 设置文件数据
* public send 发送数据
* private connect 创建连接
* private disconnect 断开连接
* private sendGet get 方式,处理发送的数据,不会处理文件数据
* private sendPost post 方式,处理发送的数据
* private sendMultipart multipart 方式,处理发送的数据,发送文件推荐使用此方式
*/
class HttpRequest{ // class start
private $_ip = '';
private $_host = '';
private $_url = '';
private $_port = '';
private $_errno = '';
private $_errstr = '';
private $_timeout = 15;
private $_fp = null;
private $_formdata = array();
private $_filedata = array();
// 设置连接参数
public function setConfig($config){
$this->_ip = isset($config['ip'])? $config['ip'] : '';
$this->_host = isset($config['host'])? $config['host'] : '';
$this->_url = isset($config['url'])? $config['url'] : '';
$this->_port = isset($config['port'])? $config['port'] : '';
$this->_errno = isset($config['errno'])? $config['errno'] : '';
$this->_errstr = isset($config['errstr'])? $config['errstr'] : '';
$this->_timeout = isset($confg['timeout'])? $confg['timeout'] : 15;
// 如没有设置ip,则用host代替
if($this->_ip==''){
$this->_ip = $this->_host;
}
}
// 设置表单数据
public function setFormData($formdata=array()){
$this->_formdata = $formdata;
}
// 设置文件数据
public function setFileData($filedata=array()){
$this->_filedata = $filedata;
}
// 发送数据
public function send($type='get'){
$type = strtolower($type);
// 检查发送类型
if(!in_array($type, array('get','post','multipart'))){
return false;
}
// 检查连接
if($this->connect()){
switch($type){
case 'get':
$out = $this->sendGet();
break;
case 'post':
$out = $this->sendPost();
break;
case 'multipart':
$out = $this->sendMultipart();
break;
}
// 空数据
if(!$out){
return false;
}
// echo $out;
// 发送数据
fputs($this->_fp, $out);
// 读取返回数据
$response = '';
while($row = fread($this->_fp, 4096)){
$response .= $row;
}
// 断开连接
$this->disconnect();
$pos = strpos($response, "\r\n\r\n");
$response = substr($response, $pos+4);
return $response;
}else{
return false;
}
}
// 创建连接
private function connect(){
$this->_fp = fsockopen($this->_ip, $this->_port, $this->_errno, $this->_errstr, $this->_timeout);
if(!$this->_fp){
return false;
}
return true;
}
// 断开连接
private function disconnect(){
if($this->_fp!=null){
fclose($this->_fp);
$this->_fp = null;
}
}
// get 方式,处理发送的数据,不会处理文件数据
private function sendGet(){
// 检查是否空数据
if(!$this->_formdata){
return false;
}
// 处理url
$url = $this->_url.'?'.http_build_query($this->_formdata);
$out = "GET ".$url." http/1.1\r\n";
$out .= "host: ".$this->_host."\r\n";
$out .= "connection: close\r\n\r\n";
return $out;
}
// post 方式,处理发送的数据
private function sendPost(){
// 检查是否空数据
if(!$this->_formdata && !$this->_filedata){
return false;
}
// form data
$data = $this->_formdata? $this->_formdata : array();
// file data
if($this->_filedata){
foreach($this->_filedata as $filedata){
if(file_exists($filedata['path'])){
$data[$filedata['name']] = file_get_contents($filedata['path']);
}
}
}
if(!$data){
return false;
}
$data = http_build_query($data);
$out = "POST ".$this->_url." http/1.1\r\n";
$out .= "host: ".$this->_host."\r\n";
$out .= "content-type: application/x-www-form-urlencoded\r\n";
$out .= "content-length: ".strlen($data)."\r\n";
$out .= "connection: close\r\n\r\n";
$out .= $data;
return $out;
}
// multipart 方式,处理发送的数据,发送文件推荐使用此方式
private function sendMultipart(){
// 检查是否空数据
if(!$this->_formdata && !$this->_filedata){
return false;
}
// 设置分割标识
srand((double)microtime()*1000000);
$boundary = '---------------------------'.substr(md5(rand(0,32000)),0,10);
$data = '--'.$boundary."\r\n";
// form data
$formdata = '';
foreach($this->_formdata as $key=>$val){
$formdata .= "content-disposition: form-data; name=\"".$key."\"\r\n";
$formdata .= "content-type: text/plain\r\n\r\n";
if(is_array($val)){
$formdata .= json_encode($val)."\r\n"; // 数组使用json encode后方便处理
}else{
$formdata .= rawurlencode($val)."\r\n";
}
$formdata .= '--'.$boundary."\r\n";
}
// file data
$filedata = '';
foreach($this->_filedata as $val){
if(file_exists($val['path'])){
$filedata .= "content-disposition: form-data; name=\"".$val['name']."\"; filename=\"".$val['filename']."\"\r\n";
$filedata .= "content-type: ".mime_content_type($val['path'])."\r\n\r\n";
// $filedata .= implode('', file($val['path']))."\r\n";
$filedata .= file_get_contents($val['path'])."\r\n";
$filedata .= '--'.$boundary."\r\n";
}
}
if(!$formdata && !$filedata){
return false;
}
$data .= $formdata.$filedata."--\r\n\r\n";
$out = "POST ".$this->_url." http/1.1\r\n";
$out .= "host: ".$this->_host."\r\n";
$out .= "content-type: multipart/form-data; boundary=".$boundary."\r\n";
$out .= "content-length: ".strlen($data)."\r\n";
$out .= "connection: close\r\n\r\n";
$out .= $data;
return $out;
}
} // class end
$config = array(
'ip' => '',
'host' => 'www.example.com',
'port' => 80,
'errno' => '',
'errstr' => '',
'timeout' => 30,
'url' => '/upload.php',
);
$formdata = array(
'name' => 'wechat',
'gender' => 'man'
);
$filedata = array(
array(
'name' => 'photo',
'filename' => 'wechat.jpg',
'path' => 'wechat.jpg'
)
);
$obj = new HttpRequest();
$obj->setConfig($config);
$obj->setFormData($formdata);
$obj->setFileData($filedata);
$result = $obj->send('get');
/*
get请求数据结构
GET /upload.php?name=wechat&gender=man http/1.1
host: www.vhallapp.com
connection: close
*/
$result = $obj->send('post');
/*
post请求数据结构
POST /upload.php http/1.1
host: www.vhallapp.com
content-type: application/x-www-form-urlencoded
content-length: 255810
connection: close
name=wechat&gender=man&photo=%89PNG%0D%0A%1A%0A%00%00%00%0DIHD
upload.php 处理
if ($_REQUEST['photo']) {
file_put_contents('photos.jpg',$_REQUEST['photo']);
}
*/
$result = $obj->send('multipart');
/*
multipart 请求数据结构
POST /upload.php http/1.1
host: www.vhallapp.com
content-type: multipart/form-data; boundary=---------------------------252e346444
content-length: 102588
connection: close
-----------------------------252e346444
content-disposition: form-data; name="name"
content-type: text/plain
wechat
-----------------------------252e346444
content-disposition: form-data; name="gender"
content-type: text/plain
man
-----------------------------252e346444
content-disposition: form-data; name="photo"; filename="wechat.jpg"
content-type: image/png
以下为二进制乱码
upload.php 处理
$uploaddir = './public/';
$uploadfile = $uploaddir . basename($_FILES['photo']['name']);
if (move_uploaded_file($_FILES['photo']['tmp_name'], $uploadfile)) {
echo "上传成功";
} else {
echo "上传失败";
}
*/
```
今天文章是通过一个[markdown工具](https://phodal.github.io/mdpub/)来编写的,支持代码高亮,如果你也喜欢可以尝试下。
每篇文章推荐一个好工具:在线ppt制作,推荐一个[教程](http://greyli.com/html-for-impress-js-write-slides/) 前提是需要前端基础,因为喜欢杰伦多年,所以做了个jay的页面,简直酷炫,[地址](https://sushengbuhuo.github.io/jay.html#/start)。一定使用chrome或Firefox访问,否则看不到效果的。
当然还有在线工具如[prezi](http://prezi.com) [ipresst](http://ipresst.com) [baomitu](https://ppt.baomitu.com/)
The End