This repository was archived by the owner on Jan 5, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFile.php
More file actions
217 lines (186 loc) · 3.82 KB
/
File.php
File metadata and controls
217 lines (186 loc) · 3.82 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
namespace Modulus\Support;
use Exception;
use Modulus\Support\Config;
use Modulus\Support\Extendable;
use Modulus\Support\Exceptions\UnknownStorageException;
use Modulus\Support\Exceptions\StorageUnusableException;
final class File
{
use Extendable;
/**
* $info
*
* @var string
*/
protected $info;
/**
* $disc
*
* @var string
*/
protected $disc;
/**
* $storage
*
* @var array
*/
protected $storage;
/**
* File name
*
* @var string
*/
public $name;
/**
* File type
*
* @var string
*/
public $type;
/**
* Temp name (path)
*
* @var string
*/
public $tmp_name;
/**
* File error
*
* @var int
*/
public $error;
/**
* File size
*
* @var int
*/
public $size;
/**
* __construct
*
* @param array $file
* @param string|null $storage
* @return void
*/
public function __construct(array $file, ?string $storage = null)
{
foreach ($file as $key => $value) {
$this->{$key} = $value;
}
$this->info = $file;
$this->storage = Config::get('filesystems.disks');
if ($storage != null) {
if (isset($this->storage[$storage])) {
$this->disc = $storage;
return;
}
throw new UnknownStorageException;
}
$this->disc = 'public';
}
/**
* Set default disc storage
*
* @param string $storage
* @return void
*/
public function disc(string $storage)
{
if (isset($this->storage[$storage])) {
$this->disc = strtolower($storage);
return $this;
}
throw new UnknownStorageException;
}
/**
* Create a new instance of the file class
*
* @param array $file
* @return void
*/
public static function make(array $file, ?string $disc = 'public')
{
return new File($file, $disc);
}
/**
* Get name
*
* @return string
*/
public function name(?string $name = null, ?bool $extension = false)
{
if ($name !== null) {
$this->info['name'] = $name . ($extension ? '.' . pathinfo($this->info['name'], PATHINFO_EXTENSION) : '');
return $this;
}
return $this->info['name'];
}
/**
* Move file
*
* @param string $dest
* @return void
*/
public function move(?string $dest = null)
{
if ($dest != null || $dest != '') {
$path = $this->storage[$this->disc]['root'] . DIRECTORY_SEPARATOR . $this->cleanPath($dest);
if (!$this->__dir($path)) throw new StorageUnusableException;
$file = $path . DIRECTORY_SEPARATOR . $this->info['name'];
} else {
$file = $this->storage[$this->disc]['root'] . DIRECTORY_SEPARATOR . $this->info['name'];
}
if (file_exists($file)) {
return [
'status' => 'failed',
'reason' => 'file already exists',
];
}
if (move_uploaded_file($this->info['tmp_name'], $file)) {
/**
* For some weird reason, sometimes files don't get
* moved. So let's run this again and hope this file
* will get moved
*/
if (!file_exists($file)) {
move_uploaded_file($this->info['tmp_name'], $file);
}
return [
'status' => 'success',
'path' => $file,
];
}
else {
return [
'status' => 'failed',
'reason' => 'something went wrong',
];
}
}
/**
* cleanPath
*
* @param string $dest
* @return void
*/
private function cleanPath(string $dest)
{
$dest = substr($dest, 0, 1) == DIRECTORY_SEPARATOR ? substr($dest, 1) : $dest;
return $dest[strlen($dest) - 1] == '/' ? substr($dest, 0, strlen($dest) - 1) : $dest;
}
/**
* __dir
*
* @param string $dir
* @return void
*/
private function __dir(string $dir) : bool
{
if (!is_dir($dir)) {
mkdir($dir, 0777, true);
}
if (is_dir($dir)) return true;
return false;
}
}