Skip to content

Commit a73c426

Browse files
committed
Provide more details in error message on failed media upload
1 parent 3e8644d commit a73c426

File tree

1 file changed

+79
-50
lines changed

1 file changed

+79
-50
lines changed

admin/controller/media/media.php

+79-50
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
use Vvveb\System\Core\View;
2929

3030
class Media extends Base {
31-
protected $uploadDenyExtensions = ['php'];
31+
protected $uploadDenyExtensions = ['php', 'svg', 'js'];
3232

3333
//protected $uploadAllowExtensions = ['ico','jpg','jpeg','png','gif','webp', 'mp4', 'mkv', 'mov'];
3434

@@ -68,48 +68,88 @@ function index() {
6868

6969
function upload() {
7070
$path = sanitizeFileName($this->request->post['mediaPath']);
71-
$file = $this->request->files['file'];
71+
$file = $this->request->files['file'] ?? [];
7272
$fileName = sanitizeFileName($file['name']);
7373
$path = preg_replace('@^/public/media|^/media|^/public@', '', $path);
7474
$extension = strtolower(substr($fileName, strrpos($fileName, '.') + 1));
75+
$success = false;
76+
$return = '';
77+
$message = '';
78+
79+
if ($file) {
80+
switch ($file['error']) {
81+
case UPLOAD_ERR_OK:
82+
$success = true;
83+
break;
84+
85+
case UPLOAD_ERR_NO_FILE:
86+
$message = __('No file sent');
87+
break;
88+
89+
case UPLOAD_ERR_PARTIAL:
90+
$message = __('The uploaded file was only partially uploaded');
91+
break;
92+
93+
case UPLOAD_ERR_NO_TMP_DIR:
94+
$message = __('Missing a temporary folder');
95+
break;
96+
97+
case UPLOAD_ERR_CANT_WRITE:
98+
$message = __('Failed to write file to disk');
99+
break;
100+
101+
case UPLOAD_ERR_EXTENSION:
102+
$message = __('A PHP extension stopped the file upload');
103+
break;
104+
105+
case UPLOAD_ERR_INI_SIZE:
106+
case UPLOAD_ERR_FORM_SIZE:
107+
$message = __('Exceeded filesize limit');
108+
break;
109+
110+
default:
111+
$message = __('Unknown errors');
112+
}
113+
114+
if (in_array($extension, $this->uploadDenyExtensions)) {
115+
$message = __('File type not allowed!');
116+
$success = false;
117+
}
75118

76-
if (in_array($extension, $this->uploadDenyExtensions)) {
77-
die(__('File type not allowed!'));
78-
}
79-
80-
switch ($file['error']) {
81-
case UPLOAD_ERR_OK:
82-
break;
83-
84-
case UPLOAD_ERR_NO_FILE:
85-
die(__('No file sent.'));
86-
87-
case UPLOAD_ERR_INI_SIZE:
88-
case UPLOAD_ERR_FORM_SIZE:
89-
die(__('Exceeded filesize limit.'));
90-
91-
default:
92-
die(__('Unknown errors.'));
93-
}
94-
95-
$origFilename = $fileName;
96-
$i = 1;
97-
98-
while (file_exists($destination = DIR_MEDIA . $path . DS . $fileName) && ($i++ < 5)) {
99-
$fileName = rand(0, 10000) . '-' . $origFilename;
100-
}
119+
$origFilename = $fileName;
120+
$i = 1;
121+
122+
if ($success) {
123+
while (file_exists($destination = DIR_MEDIA . $path . DS . $fileName) && ($i++ < 5)) {
124+
$fileName = rand(0, 10000) . '-' . $origFilename;
125+
}
101126

102-
if (move_uploaded_file($file['tmp_name'], $destination)) {
103-
if (isset($this->request->post['onlyFilename'])) {
104-
echo $fileName;
105-
} else {
106-
echo $destination;
127+
if (move_uploaded_file($file['tmp_name'], $destination)) {
128+
if (isset($this->request->post['onlyFilename'])) {
129+
$return = $fileName;
130+
} else {
131+
$return = $destination;
132+
}
133+
$message = __('File uploaded successfully!');
134+
} else {
135+
$destination = DIR_MEDIA . $path . DS;
136+
$success = false;
137+
138+
if (!is_writable($destination)) {
139+
$message = sprintf(__('%s not writable!'), $destination);
140+
} else {
141+
$message = __('Error moving uploaded file!');
142+
}
143+
}
107144
}
108145
} else {
109-
echo __('Error uploading file!');
146+
$message = __('Invalid upload!');
110147
}
111-
112-
die();
148+
149+
$message = ['success' => true, 'message' => $message, 'file' => $return];
150+
151+
$this->response->setType('json');
152+
$this->response->output($message);
113153
}
114154

115155
function delete() {
@@ -124,9 +164,8 @@ function delete() {
124164
$message = ['success' => false, 'message' => __('Error deleting file!')];
125165
}
126166

127-
echo json_encode($message);
128-
129-
die();
167+
$this->response->setType('json');
168+
$this->response->output($message);
130169
}
131170

132171
function rename() {
@@ -153,9 +192,9 @@ function rename() {
153192
}
154193
}
155194

156-
echo json_encode($message);
157195

158-
die();
196+
$this->response->setType('json');
197+
$this->response->output($message);
159198
}
160199

161200
function scan() {
@@ -217,15 +256,5 @@ function scan() {
217256
'path' => '',
218257
'items' => $response,
219258
]);
220-
/*
221-
$view = View::getInstance();
222-
$view->set([
223-
'name' => '',
224-
'type' => 'folder',
225-
'path' => '',
226-
'items' => $response,
227-
]);
228-
*/
229-
return;
230259
}
231260
}

0 commit comments

Comments
 (0)