From 360a7c112fbff3ccc3cb7949f130bcf7a0119e96 Mon Sep 17 00:00:00 2001 From: lionel Date: Mon, 25 Jan 2021 15:27:10 +0100 Subject: [PATCH] Maximum Upload --- fonction.php | 39 +++++++++++++++++++++++++++++++++++++++ upload.php | 23 +++++++++++------------ 2 files changed, 50 insertions(+), 12 deletions(-) create mode 100644 fonction.php diff --git a/fonction.php b/fonction.php new file mode 100644 index 0000000..fc8791e --- /dev/null +++ b/fonction.php @@ -0,0 +1,39 @@ + \ No newline at end of file diff --git a/upload.php b/upload.php index 16431ed..f89a860 100644 --- a/upload.php +++ b/upload.php @@ -4,13 +4,16 @@ error_reporting(E_ALL); ini_set('display_errors', TRUE); ini_set('display_startup_errors', TRUE); +include 'fonction.php'; + +$max_size = convertToBytes('2GB'); + $root = $_SERVER["DOCUMENT_ROOT"]; $target_dir = "$root/uploads/"; if(! isset($_FILES["file"])) { header("HTTP/1.1 400 No file Upload"); - //trigger_error("No file Upload",E_USER_ERROR); exit(); } @@ -18,9 +21,15 @@ $file = $_FILES["file"]; $name = basename($file["name"]); +$size = basename($file["size"]); + +if ((folderSize($target_dir) + $size) > $max_size) { + header("HTTP/1.1 400 " . $name . " file too large"); + exit(); +} if (! extension_loaded('mbstring')) { - header("HTTP/1.1 400 Bad encode"); + header("HTTP/1.1 500 php module missing"); exit(); } @@ -29,9 +38,6 @@ if(! mb_check_encoding($name)) { exit(); } - -$size = basename($file["size"]); - $target_file = $target_dir.$name; // Check if file already exists @@ -48,11 +54,4 @@ else { } } -function formatBytes($size, $precision = 2) -{ - $base = log($size, 1024); - $suffixes = array('', 'KiB', 'MiB', 'GiB', 'TiB'); - - return round(pow(1024, $base - floor($base)), $precision) .' '. $suffixes[floor($base)]; -} ?>