Skip to content

Commit 8bd8691

Browse files
committed
Refactor file upload error handling for early exits
1 parent 3608709 commit 8bd8691

File tree

1 file changed

+20
-15
lines changed

1 file changed

+20
-15
lines changed

main/inc/ajax/document.ajax.php

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -224,14 +224,17 @@
224224
false,
225225
'files'
226226
);
227-
if ($result) {
228-
$relativeUrl = str_replace(api_get_path(WEB_PATH), '/', $result['direct_url']);
229-
$data = [
230-
'uploaded' => 1,
231-
'fileName' => $fileUpload['name'],
232-
'url' => $relativeUrl,
233-
];
227+
228+
if (!$result) {
229+
exit;
234230
}
231+
232+
$relativeUrl = str_replace(api_get_path(WEB_PATH), '/', $result['direct_url']);
233+
$data = [
234+
'uploaded' => 1,
235+
'fileName' => $fileUpload['name'],
236+
'url' => $relativeUrl,
237+
];
235238
} else {
236239
$userId = api_get_user_id();
237240
$syspath = UserManager::getUserPathById($userId, 'system').'my_files';
@@ -246,15 +249,17 @@
246249
$suffix = '_'.uniqid();
247250
$fileUploadName = $fileName.$suffix.'.'.$extension;
248251
}
249-
if (move_uploaded_file($fileUpload['tmp_name'], $syspath.$fileUploadName)) {
250-
$url = $webpath.$fileUploadName;
251-
$relativeUrl = str_replace(api_get_path(WEB_PATH), '/', $url);
252-
$data = [
253-
'uploaded' => 1,
254-
'fileName' => $fileUploadName,
255-
'url' => $relativeUrl,
256-
];
252+
if (!move_uploaded_file($fileUpload['tmp_name'], $syspath . $fileUploadName)) {
253+
exit;
257254
}
255+
256+
$url = $webpath . $fileUploadName;
257+
$relativeUrl = str_replace(api_get_path(WEB_PATH), '/', $url);
258+
$data = [
259+
'uploaded' => 1,
260+
'fileName' => $fileUploadName,
261+
'url' => $relativeUrl,
262+
];
258263
}
259264
echo json_encode($data);
260265
exit;

0 commit comments

Comments
 (0)