-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUploadTool.php
101 lines (92 loc) · 2.77 KB
/
UploadTool.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
<?php
namespace Ling\Bat;
/**
* MODELS
* ===================
*
* phpFile
* ---------------
* array:
* - name
* - tmp_name
* - error
* - type
* - size
*
*
* combinedStructure
* -------------------
* array:
* - name: array of php file names
* - type: array of php file mime types
* - tmp_name: array of php file temporary locations
* - error: array of php file error indicators
* - size: array of php file sizes
*/
class UploadTool
{
/**
* @param array $phpFile , a phpFile model (see at the top of this class)
* @return bool
*/
public static function isValid(array $phpFile)
{
if (
array_key_exists('error', $phpFile) &&
0 === (int)$phpFile['error'] &&
array_key_exists('name', $phpFile) &&
array_key_exists('tmp_name', $phpFile) &&
array_key_exists('size', $phpFile) &&
array_key_exists('type', $phpFile)
) {
return true;
}
return false;
}
/**
* @param array $combineStructure :
* name: array of php file names
* type: array of php file mime types
* tmp_name: array of php file temporary locations
* error: array of php file error indicators
* size: array of php file sizes
*
* @return array of phpFile (see top of this class for more info)
*/
public static function getPhpFilesArrayFromCombinedStructure(array $combineStructure)
{
$ret = [];
foreach ($combineStructure as $key => $values) {
foreach ($values as $index => $value) {
$ret[$index][$key] = $value;
}
}
return $ret;
}
/**
*
*
* @param array $phpFilesItem , a regular $_FILES item,
* which can have one of two structure:
* - single file upload (a phpFile item as described at the top of this class)
* - multiple file upload ( a combined structure as described at the top of this class)
*
* @return array|false an array of phpFile items (as described at the top of this class).
* false is returned if the given array is empty
*/
public static function getPhpFilesArrayFromFilesSuperArrayItem(array $phpFilesItem)
{
if ($phpFilesItem) {
foreach ($phpFilesItem as $key => $values) {
if (is_array($values)) {
// this is a combined structure
return self::getPhpFilesArrayFromCombinedStructure($phpFilesItem);
} else {
// this is single file structure
return [$phpFilesItem];
}
}
}
return false;
}
}