1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2019 Amin Yazdanpanah<http://www.aminyazdanpanah.com>. |
5
|
|
|
* |
6
|
|
|
* Licensed under the MIT License; |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* https://opensource.org/licenses/MIT |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace AYazdanpanah\SaveUploadedFiles; |
20
|
|
|
|
21
|
|
|
|
22
|
|
|
use AYazdanpanah\SaveUploadedFiles\Exception\Exception; |
23
|
|
|
|
24
|
|
|
class File extends Save |
25
|
|
|
{ |
26
|
|
|
private $filename; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param $filename |
30
|
|
|
* @return File |
31
|
|
|
*/ |
32
|
|
|
public function file($filename) |
33
|
|
|
{ |
34
|
|
|
$this->filename = $filename; |
35
|
|
|
return $this; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @return mixed |
40
|
|
|
* @throws Exception |
41
|
|
|
*/ |
42
|
|
|
private function getFile() |
43
|
|
|
{ |
44
|
|
|
if (!isset($_FILES[$this->filename])) { |
45
|
|
|
throw new Exception("There is no \"" .$this->filename . "\" key in the \$_FILES. Be sure your key name is correct"); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
if($code = $_FILES[$this->filename]['error']){ |
49
|
|
|
$this->throwErrorException($code); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $_FILES[$this->filename]; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return mixed |
57
|
|
|
* @throws Exception |
58
|
|
|
*/ |
59
|
|
|
public function getFileName() |
60
|
|
|
{ |
61
|
|
|
return explode('.', $this->getFile()['name'])[0]; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @return mixed |
66
|
|
|
* @throws Exception |
67
|
|
|
*/ |
68
|
|
|
public function getFileType() |
69
|
|
|
{ |
70
|
|
|
return $this->getFile()['type']; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return mixed |
75
|
|
|
* @throws Exception |
76
|
|
|
*/ |
77
|
|
|
public function getFileTmpName() |
78
|
|
|
{ |
79
|
|
|
return $this->getFile()['tmp_name']; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return mixed |
84
|
|
|
* @throws Exception |
85
|
|
|
*/ |
86
|
|
|
public function getFileSize() |
87
|
|
|
{ |
88
|
|
|
return $this->getFile()['size']; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return mixed |
93
|
|
|
* @throws Exception |
94
|
|
|
*/ |
95
|
|
|
public function getBaseNameFile() |
96
|
|
|
{ |
97
|
|
|
return basename($this->getFile()['name']); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return mixed |
102
|
|
|
* @throws Exception |
103
|
|
|
*/ |
104
|
|
|
public function getFileExtension() |
105
|
|
|
{ |
106
|
|
|
$name = explode('.', $this->getFile()['name']); |
107
|
|
|
return end($name); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param $code |
112
|
|
|
* @throws Exception |
113
|
|
|
*/ |
114
|
|
|
private function throwErrorException($code) |
115
|
|
|
{ |
116
|
|
|
switch ($code){ |
117
|
|
|
case 1: |
118
|
|
|
throw new Exception("The uploaded file exceeds the upload_max_filesize directive in php.ini"); |
119
|
|
|
break; |
120
|
|
|
case 2: |
121
|
|
|
throw new Exception("The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form"); |
122
|
|
|
break; |
123
|
|
|
case 3: |
124
|
|
|
throw new Exception("The uploaded file was only partially uploaded"); |
125
|
|
|
break; |
126
|
|
|
case 4: |
127
|
|
|
throw new Exception("No file was uploaded"); |
128
|
|
|
break; |
129
|
|
|
case 5: |
130
|
|
|
throw new Exception("Unknown error occurred."); |
131
|
|
|
break; |
132
|
|
|
case 6: |
133
|
|
|
throw new Exception("Missing a temporary folder."); |
134
|
|
|
break; |
135
|
|
|
case 7: |
136
|
|
|
throw new Exception("Failed to write file to disk"); |
137
|
|
|
break; |
138
|
|
|
case 8: |
139
|
|
|
throw new Exception("A PHP extension stopped the file upload"); |
140
|
|
|
break; |
141
|
|
|
default: |
142
|
|
|
throw new Exception("Unknown error occurred."); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
|
146
|
|
|
} |
147
|
|
|
} |