|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @package CleverStyle CMS |
|
4
|
|
|
* @author Nazar Mokrynskyi <[email protected]> |
|
5
|
|
|
* @copyright Copyright (c) 2016, Nazar Mokrynskyi |
|
6
|
|
|
* @license MIT License, see license.txt |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace cs\Request; |
|
9
|
|
|
|
|
10
|
|
|
trait Files { |
|
11
|
|
|
/** |
|
12
|
|
|
* Normalized files array |
|
13
|
|
|
* |
|
14
|
|
|
* Each file item can be either single file or array of files (in contrast with native PHP arrays where each field like `name` become an array) with keys |
|
15
|
|
|
* `name`, `type`, `size`, `tmp_name`, `stream` and `error` |
|
16
|
|
|
* |
|
17
|
|
|
* `name`, `type`, `size` and `error` keys are similar to native PHP fields in `$_FILES`; `tmp_name` might not be temporary file, but file descriptor |
|
18
|
|
|
* wrapper like `php://fd/1` and `stream` is resource like obtained with `fopen('/tmp/xyz')` |
|
19
|
|
|
* |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
public $files; |
|
23
|
|
|
/** |
|
24
|
|
|
* @param array $files Typically `$_FILES`; might be like native PHP array `$_FILES` or normalized; each file item MUST contain keys `name`, `type`, `size`, |
|
25
|
|
|
* `error` and at least one of `tmp_name` or `stream` |
|
26
|
|
|
*/ |
|
27
|
|
|
function init_files ($files = []) { |
|
28
|
|
|
$this->files = $this->init_files_internal($files); |
|
29
|
|
|
} |
|
30
|
|
|
/** |
|
31
|
|
|
* @param array $files |
|
32
|
|
|
* |
|
33
|
|
|
* @return array |
|
34
|
|
|
*/ |
|
35
|
|
|
protected function init_files_internal ($files) { |
|
36
|
|
|
if (!isset($files['name'])) { |
|
37
|
|
|
foreach ($files as $field => &$file) { |
|
38
|
|
|
$file = $this->init_files_internal($file); |
|
|
|
|
|
|
39
|
|
|
} |
|
40
|
|
|
return $files; |
|
41
|
|
|
} |
|
42
|
|
|
if (is_array($files['name'])) { |
|
43
|
|
|
$result = []; |
|
44
|
|
|
foreach (array_keys($files['name']) as $index) { |
|
45
|
|
|
$result[] = $this->normalize_file( |
|
46
|
|
|
[ |
|
47
|
|
|
'name' => $files['name'][$index], |
|
48
|
|
|
'type' => $files['type'][$index], |
|
49
|
|
|
'size' => $files['size'][$index], |
|
50
|
|
|
'tmp_name' => @$files['tmp_name'][$index] ?: null, |
|
51
|
|
|
'stream' => @$files['stream'][$index] ?: null, |
|
52
|
|
|
'error' => $files['error'][$index] |
|
53
|
|
|
] |
|
54
|
|
|
); |
|
55
|
|
|
} |
|
56
|
|
|
return $result; |
|
57
|
|
|
} else { |
|
58
|
|
|
return $this->normalize_file($files); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
/** |
|
62
|
|
|
* @param array $file |
|
63
|
|
|
* |
|
64
|
|
|
* @return array |
|
65
|
|
|
*/ |
|
66
|
|
|
protected function normalize_file ($file) { |
|
67
|
|
|
$file += [ |
|
68
|
|
|
'tmp_name' => null, |
|
69
|
|
|
'stream' => null |
|
70
|
|
|
]; |
|
71
|
|
|
if (isset($file['tmp_name']) && $file['stream'] === null) { |
|
72
|
|
|
$file['stream'] = fopen($file['tmp_name'], 'br'); |
|
73
|
|
|
} |
|
74
|
|
|
if (isset($file['stream']) && $file['tmp_name'] === null) { |
|
75
|
|
|
$file['tmp_name'] = "php://fd/$file[stream]"; |
|
76
|
|
|
} |
|
77
|
|
|
if ($file['tmp_name'] === null && $file['stream'] === null) { |
|
78
|
|
|
$file['error'] = UPLOAD_ERR_NO_FILE; |
|
79
|
|
|
} |
|
80
|
|
|
return $file; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: