Skip to content

Commit

Permalink
implemented better error messages and fixed upload size errors
Browse files Browse the repository at this point in the history
  • Loading branch information
geek-at committed Dec 7, 2024
1 parent e839482 commit 431a099
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
3 changes: 3 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ RUN mkdir -p /var/log/nginx
# php stuff
RUN sed -i 's/nobody/nginx/g' /etc/php83/php-fpm.d/www.conf
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
# disable upload file size limit
RUN sed -i 's/upload_max_filesize = 2M/upload_max_filesize = 0/g' /etc/php83/php.ini
RUN sed -i 's/post_max_size = 8M/post_max_size = 0/g' /etc/php83/php.ini

# web interface stuff
WORKDIR /var/www/backupdrop/web/lib
Expand Down
10 changes: 8 additions & 2 deletions web/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,14 @@ function handleUpload($hostname)
}
else //some upload error
{
http_response_code(404);
return ['status'=>'error','reason'=>'No file uploaded'];
$error = $_FILES["file"]["error"];
if($error == 1 || $error == 2)
http_response_code(413);
else if($error == 3)
http_response_code(500);
else
http_response_code(404);
return ['status'=>'error','reason'=>'No file uploaded','error'=>uploadErrorTranslator($error)];
}
}

Expand Down
14 changes: 14 additions & 0 deletions web/lib/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,18 @@ function endsWith( $haystack, $needle ) {
return true;
}
return substr( $haystack, -$length ) === $needle;
}

function uploadErrorTranslator($code){
return match ($code)
{
0 => 'There is no error, the file uploaded with success',
1 => 'The uploaded file exceeds the upload_max_filesize directive in php.ini',
2 => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form',
3 => 'The uploaded file was only partially uploaded',
4 => 'No file was uploaded',
6 => 'Missing a temporary folder',
7 => 'Failed to write file to disk.',
8 => 'A PHP extension stopped the file upload.'
};
}

0 comments on commit 431a099

Please sign in to comment.