Skip to content

Commit 431a099

Browse files
committed
implemented better error messages and fixed upload size errors
1 parent e839482 commit 431a099

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

docker/Dockerfile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ RUN mkdir -p /var/log/nginx
2323
# php stuff
2424
RUN sed -i 's/nobody/nginx/g' /etc/php83/php-fpm.d/www.conf
2525
RUN sed -i 's/E_ALL \& ~E_DEPRECATED \& ~E_STRICT/E_ALL \& ~E_DEPRECATED \& ~E_STRICT \& ~E_NOTICE \& ~E_WARNING/g' /etc/php83/php.ini
26+
# disable upload file size limit
27+
RUN sed -i 's/upload_max_filesize = 2M/upload_max_filesize = 0/g' /etc/php83/php.ini
28+
RUN sed -i 's/post_max_size = 8M/post_max_size = 0/g' /etc/php83/php.ini
2629

2730
# web interface stuff
2831
WORKDIR /var/www/backupdrop/web/lib

web/index.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,14 @@ function handleUpload($hostname)
8282
}
8383
else //some upload error
8484
{
85-
http_response_code(404);
86-
return ['status'=>'error','reason'=>'No file uploaded'];
85+
$error = $_FILES["file"]["error"];
86+
if($error == 1 || $error == 2)
87+
http_response_code(413);
88+
else if($error == 3)
89+
http_response_code(500);
90+
else
91+
http_response_code(404);
92+
return ['status'=>'error','reason'=>'No file uploaded','error'=>uploadErrorTranslator($error)];
8793
}
8894
}
8995

web/lib/helpers.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,18 @@ function endsWith( $haystack, $needle ) {
7272
return true;
7373
}
7474
return substr( $haystack, -$length ) === $needle;
75+
}
76+
77+
function uploadErrorTranslator($code){
78+
return match ($code)
79+
{
80+
0 => 'There is no error, the file uploaded with success',
81+
1 => 'The uploaded file exceeds the upload_max_filesize directive in php.ini',
82+
2 => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form',
83+
3 => 'The uploaded file was only partially uploaded',
84+
4 => 'No file was uploaded',
85+
6 => 'Missing a temporary folder',
86+
7 => 'Failed to write file to disk.',
87+
8 => 'A PHP extension stopped the file upload.'
88+
};
7589
}

0 commit comments

Comments
 (0)