|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ntentan\utils\filesystem; |
|
4
|
|
|
|
|
5
|
|
|
use ntentan\utils\exceptions\FilesystemException; |
|
6
|
|
|
use ntentan\utils\Filesystem; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Represents a file that was uploaded through PHPs internal HTTP mechanisms. |
|
10
|
|
|
* |
|
11
|
|
|
* @package ntentan\utils\filesystem |
|
12
|
|
|
*/ |
|
13
|
|
|
class UploadedFile extends File |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Filename of the file from the client. |
|
17
|
|
|
* |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
private $clientName; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* File type. |
|
24
|
|
|
* |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
private $type; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Any upload errors that occured. |
|
31
|
|
|
* These are based on the errors described for PHPs $_FILES global variable. |
|
32
|
|
|
* |
|
33
|
|
|
* @var int |
|
34
|
|
|
*/ |
|
35
|
|
|
private $error; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Size reported when file was uploaded. |
|
39
|
|
|
* |
|
40
|
|
|
* @var int |
|
41
|
|
|
*/ |
|
42
|
|
|
private $size; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Create a new instance. |
|
46
|
|
|
* |
|
47
|
|
|
* @param array $file The $_FILES[name] value for a given upload field |
|
48
|
|
|
* @throws FilesystemException |
|
49
|
|
|
*/ |
|
50
|
5 |
|
public function __construct(array $file = null) |
|
51
|
|
|
{ |
|
52
|
5 |
|
if($file === null) { |
|
53
|
|
|
throw new FilesystemException("Failed to complete file upload"); |
|
54
|
5 |
|
} else if (!is_uploaded_file($file['tmp_name'])) { |
|
55
|
1 |
|
throw new FilesystemException("File {$file['tmp_name']} is not an uploaded file."); |
|
56
|
|
|
} |
|
57
|
4 |
|
parent::__construct($file['tmp_name']); |
|
58
|
4 |
|
$this->clientName = $file['name']; |
|
59
|
4 |
|
$this->type = $file['type']; |
|
60
|
4 |
|
$this->error = $file['error']; |
|
61
|
4 |
|
$this->size = $file['size']; |
|
62
|
4 |
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Get the size of the uploaded file as reported by PHP. |
|
66
|
|
|
* |
|
67
|
|
|
* @return int |
|
68
|
|
|
*/ |
|
69
|
1 |
|
public function getSize(): int |
|
70
|
|
|
{ |
|
71
|
1 |
|
return $this->size; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Move the uploaded file safely to another location. |
|
76
|
|
|
* Ensures that files were actually uploaded through PHP before moving them. |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $destination |
|
79
|
|
|
* @param string $overwrite |
|
80
|
|
|
* @throws FilesystemException |
|
81
|
|
|
* @throws \ntentan\utils\exceptions\FileNotWriteableException |
|
82
|
|
|
*/ |
|
83
|
3 |
|
public function moveTo(string $destination, int $overwrite = self::OVERWRITE_ALL): void |
|
84
|
|
|
{ |
|
85
|
3 |
|
$destination = is_dir($destination) ? ("$destination/{$this->clientName}") : $destination; |
|
86
|
3 |
|
Filesystem::checkWritable(dirname($destination)); |
|
87
|
3 |
|
if (!move_uploaded_file($this->path, $destination)) { |
|
88
|
1 |
|
throw new FilesystemException("Failed to move file {$this->path} to {$destination}"); |
|
89
|
|
|
} |
|
90
|
2 |
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Return the error code assigned when file was uploaded. |
|
94
|
|
|
* See the documentation for the $_FILES global variable for a description of this value. |
|
95
|
|
|
* |
|
96
|
|
|
* @return int |
|
97
|
|
|
*/ |
|
98
|
1 |
|
public function getError(): int |
|
99
|
|
|
{ |
|
100
|
1 |
|
return $this->error; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Get the name of the file assigned from the client system. |
|
105
|
|
|
* |
|
106
|
|
|
* @return string |
|
107
|
|
|
*/ |
|
108
|
1 |
|
public function getClientName(): string |
|
109
|
|
|
{ |
|
110
|
1 |
|
return $this->clientName; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Get the mime type of the file. |
|
115
|
|
|
* |
|
116
|
|
|
* @return string |
|
117
|
|
|
*/ |
|
118
|
1 |
|
public function getType(): string |
|
119
|
|
|
{ |
|
120
|
1 |
|
return $this->type; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
} |
|
124
|
|
|
|