1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Util\Uploaded |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2010-2013 Gjero Krsteski (http://krsteski.de) |
6
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
7
|
|
|
*/ |
8
|
|
|
namespace Pimf\Util\Uploaded; |
9
|
|
|
|
10
|
|
|
use \Pimf\Util\Uploaded; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* A file uploaded through a form. |
14
|
|
|
* |
15
|
|
|
* <code> |
16
|
|
|
* |
17
|
|
|
* // Create an instance using the factory method for more security. |
18
|
|
|
* $upload = \Pimf\Util\Uploaded\Factory::get($_FILES); |
19
|
|
|
* |
20
|
|
|
* if ($upload instanceof Uploaded) { |
21
|
|
|
* $upload->move('path/to/your/images/dir', $upload->getClientOriginalName()); |
22
|
|
|
* } |
23
|
|
|
* |
24
|
|
|
* </code> |
25
|
|
|
* |
26
|
|
|
* @package Util |
27
|
|
|
* @author Gjero Krsteski <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
abstract class Factory |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
protected static $fileKeys = array('error', 'name', 'size', 'tmp_name', 'type'); |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Factory for save instance creation. |
38
|
|
|
* |
39
|
|
|
* <code> |
40
|
|
|
* // Create an instance using the factory method. |
41
|
|
|
* $file = \Pimf\Util\Uploaded\Factory::get($_FILES); |
42
|
|
|
* </code> |
43
|
|
|
* |
44
|
|
|
* @param mixed $file A $_FILES multi-dimensional array of uploaded file information. |
45
|
|
|
* @param bool $test Whether the test mode is active for essayer unit-testing. |
46
|
|
|
* |
47
|
|
|
* @return null|Uploaded |
48
|
|
|
*/ |
49
|
|
|
public static function get(array $file, $test = false) |
50
|
|
|
{ |
51
|
|
|
$file = static::heal($file); |
52
|
|
|
|
53
|
|
|
if (is_array($file) && isset($file['name']) && empty($file['name']) === false) { |
54
|
|
|
|
55
|
|
|
$keys = array_keys($file); |
56
|
|
|
sort($keys); |
57
|
|
|
|
58
|
|
|
if ($keys == self::$fileKeys) { |
59
|
|
|
|
60
|
|
|
if (UPLOAD_ERR_NO_FILE == $file['error']) { |
61
|
|
|
return null; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return new Uploaded($file['tmp_name'], $file['name'], $file['type'], $file['size'], $file['error'], |
65
|
|
|
$test); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return null; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Heals a malformed PHP $_FILES array. |
74
|
|
|
* |
75
|
|
|
* PHP has a bug that the format of the $_FILES array differs, depending on |
76
|
|
|
* whether the uploaded file fields had normal field names or array-like |
77
|
|
|
* field names ("normal" vs. "parent[child]"). |
78
|
|
|
* |
79
|
|
|
* This method fixes the array to look like the "normal" $_FILES array. |
80
|
|
|
* |
81
|
|
|
* @param array $data |
82
|
|
|
* |
83
|
|
|
* @return array |
84
|
|
|
*/ |
85
|
|
|
public static function heal($data) |
86
|
|
|
{ |
87
|
|
|
if (!is_array($data)) { |
88
|
|
|
return $data; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$keys = array_keys($data); |
92
|
|
|
sort($keys); |
93
|
|
|
|
94
|
|
|
if (self::$fileKeys != $keys || !isset($data['name']) || !is_array($data['name'])) { |
95
|
|
|
return $data; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$files = $data; |
99
|
|
|
|
100
|
|
|
foreach (self::$fileKeys as $k) { |
101
|
|
|
unset($files[$k]); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
foreach (array_keys($data['name']) as $key) { |
105
|
|
|
$files[$key] = static::heal( |
106
|
|
|
array( |
107
|
|
|
'error' => $data['error'][$key], |
108
|
|
|
'name' => $data['name'][$key], |
109
|
|
|
'type' => $data['type'][$key], |
110
|
|
|
'tmp_name' => $data['tmp_name'][$key], |
111
|
|
|
'size' => $data['size'][$key] |
112
|
|
|
) |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $files; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
} |
120
|
|
|
|