/
Upload.php
152 lines (121 loc) 路 3.2 KB
/
Upload.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
<?php
namespace Utilities;
use Core\Log;
class Upload
{
const DATE_FORMAT = 'Y-m-d H:i:s';
/**
* Maximum size for files in kB.
*
* @var int
*/
private $maxSize;
/**
* File upload directory.
*
* @var string
*/
private $directory;
/**
* Data about the last file uploaded.
*
* @var array
*/
private $lastFile;
/**
* Set the files maximum size
*
* @param float $maxSize the files maximum size
*
* @return self this
*/
public function setMaxSize(float $maxSize)
{
$this->maxSize = $maxSize * 1024;
return $this;
}
/**
* Get the files maximum size
* @return float the files maximum size
*/
public function getMaxSize()
{
return $this->maxSize;
}
/**
* Set the file upload directory
*
* @param string $directory the file upload directory
*
* @return self this
*/
public function setDirectory(string $directory)
{
$this->directory = $directory;
return $this;
}
/**
* Get the file upload directory
* @return float the file upload directory
*/
public function getDir()
{
return $this->directory ?? '';
}
/**
* Check if a file matches one or more formats
*
* @param $filename the file name
* @param $formats the formats for comparision
*
* @return bool true if the file matches the formats, false otherwise
*/
public function matchFormat($filename, $formats)
{
$file = $_FILES[$filename]['name'];
if (!is_array($formats)) {
$formats = explode(',', $formats);
}
$formats = array_map('trim', array_map('strtolower', $formats));
return in_array(pathinfo($file, PATHINFO_EXTENSION), $formats);
}
/**
* Upload a file
*
* @param string $filename the file name
*
* @return bool true if the file has been uploaded successfully, false otherwise
*/
public function file(string $filename)
{
$file = $_FILES[$filename];
if (isset($this->maxSize) && $file['size'] > $this->maxSize) {
Log::notice("File '" . $file['name'] . "' exceeds maximum upload size");
return false;
}
$dir = getPublicDirectory() . $this->directory;
if (!move_uploaded_file($file['tmp_name'], $dir . '/' . $file['name'])) {
Log::notice("Upload of '" . $file['name'] . "' failed");
return false;
}
$this->lastFile = [
'name' => $file['name'],
'type' => $file['type'],
'tmp_name' => $file['tmp_name'],
'error' => $file['error'],
'size' => $file['size'],
'directory' => $dir,
'uploader_ip' => $_SERVER['REMOTE_ADDR'],
'date' => date(self::DATE_FORMAT)
];
return true;
}
/**
* Get info about the last file uploaded
* @return array the info about the last file uploaded as an associative array
*/
public function getLastFile()
{
return $this->lastFile;
}
}