|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* FileUpload.php - This class handles HTTP file upload. |
|
5
|
|
|
* |
|
6
|
|
|
* @package jaxon-core |
|
|
|
|
|
|
7
|
|
|
* @author Thierry Feuzeu <[email protected]> |
|
8
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
|
9
|
|
|
* @link https://github.com/jaxon-php/jaxon-core |
|
10
|
|
|
*/ |
|
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
namespace Jaxon\Request\Support; |
|
13
|
|
|
|
|
14
|
|
|
use Closure; |
|
15
|
|
|
|
|
16
|
|
|
class FileUpload |
|
|
|
|
|
|
17
|
|
|
{ |
|
18
|
|
|
use \Jaxon\Features\Validator; |
|
19
|
|
|
use \Jaxon\Features\Translator; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* A user defined function to transform uploaded file names |
|
23
|
|
|
* |
|
24
|
|
|
* @var Closure |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $cNameSanitizer = null; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Filter uploaded file name |
|
30
|
|
|
* |
|
31
|
|
|
* @param Closure $cNameSanitizer The closure which filters filenames |
|
|
|
|
|
|
32
|
|
|
* |
|
33
|
|
|
* @return void |
|
34
|
|
|
*/ |
|
35
|
|
|
public function setNameSanitizer(Closure $cNameSanitizer) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->cNameSanitizer = $cNameSanitizer; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Read uploaded files info from HTTP request data |
|
42
|
|
|
* |
|
43
|
|
|
* @return void |
|
|
|
|
|
|
44
|
|
|
*/ |
|
45
|
|
|
public function getUploadedFiles() |
|
46
|
|
|
{ |
|
47
|
|
|
// Check validity of the uploaded files |
|
48
|
|
|
$aTempFiles = []; |
|
49
|
|
|
foreach($_FILES as $sVarName => $aFile) |
|
50
|
|
|
{ |
|
51
|
|
|
// If there is only one file, transform each entry into an array, |
|
52
|
|
|
// so the same processing for multiple files can be applied. |
|
53
|
|
|
if(!is_array($aFile['name'])) |
|
54
|
|
|
{ |
|
55
|
|
|
$aFile['name'] = [$aFile['name']]; |
|
|
|
|
|
|
56
|
|
|
$aFile['type'] = [$aFile['type']]; |
|
|
|
|
|
|
57
|
|
|
$aFile['tmp_name'] = [$aFile['tmp_name']]; |
|
58
|
|
|
$aFile['error'] = [$aFile['error']]; |
|
|
|
|
|
|
59
|
|
|
$aFile['size'] = [$aFile['size']]; |
|
|
|
|
|
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
$nFileCount = count($aFile['name']); |
|
63
|
|
|
for($i = 0; $i < $nFileCount; $i++) |
|
64
|
|
|
{ |
|
65
|
|
|
if(!$aFile['name'][$i]) |
|
66
|
|
|
{ |
|
67
|
|
|
continue; |
|
68
|
|
|
} |
|
69
|
|
|
if(!array_key_exists($sVarName, $aTempFiles)) |
|
70
|
|
|
{ |
|
71
|
|
|
$aTempFiles[$sVarName] = []; |
|
72
|
|
|
} |
|
73
|
|
|
// Filename without the extension |
|
74
|
|
|
$sFilename = pathinfo($aFile['name'][$i], PATHINFO_FILENAME); |
|
75
|
|
|
if(($this->cNameSanitizer)) |
|
76
|
|
|
{ |
|
77
|
|
|
$sFilename = (string)call_user_func_array($$this->cNameSanitizer, [$sFilename, $sVarName]); |
|
78
|
|
|
} |
|
79
|
|
|
// Copy the file data into the local array |
|
80
|
|
|
$aTempFiles[$sVarName][] = [ |
|
81
|
|
|
'name' => $aFile['name'][$i], |
|
82
|
|
|
'type' => $aFile['type'][$i], |
|
83
|
|
|
'tmp_name' => $aFile['tmp_name'][$i], |
|
84
|
|
|
'error' => $aFile['error'][$i], |
|
85
|
|
|
'size' => $aFile['size'][$i], |
|
86
|
|
|
'filename' => $sFilename, |
|
87
|
|
|
'extension' => pathinfo($aFile['name'][$i], PATHINFO_EXTENSION), |
|
88
|
|
|
]; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
// Check uploaded files validity |
|
93
|
|
|
foreach($aTempFiles as $sVarName => $aFiles) |
|
94
|
|
|
{ |
|
95
|
|
|
foreach($aFiles as $aFile) |
|
96
|
|
|
{ |
|
97
|
|
|
// Verify upload result |
|
98
|
|
|
if($aFile['error'] != 0) |
|
99
|
|
|
{ |
|
100
|
|
|
throw new \Jaxon\Exception\Error($this->trans('errors.upload.failed', $aFile)); |
|
101
|
|
|
} |
|
102
|
|
|
// Verify file validity (format, size) |
|
103
|
|
|
if(!$this->validateUploadedFile($sVarName, $aFile)) |
|
104
|
|
|
{ |
|
105
|
|
|
throw new \Jaxon\Exception\Error($this->getValidatorMessage()); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return $aTempFiles; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|