From 6adf0ee0a2c8629c95daf8d5842ad4df59427ae4 Mon Sep 17 00:00:00 2001 From: Joshua Ramon Enslin Date: Tue, 20 Jul 2021 01:26:31 +0200 Subject: [PATCH] Add wrapper around move_uploaded_file --- src/MD_STD.php | 19 +++++++++++++++++++ src/MD_STD_IN.php | 21 +++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/src/MD_STD.php b/src/MD_STD.php index 209a1d3..219edde 100644 --- a/src/MD_STD.php +++ b/src/MD_STD.php @@ -679,4 +679,23 @@ final class MD_STD { return $code; } + + /** + * Checks if a directory is writable. + * + * @param string $dir Directory path. + * + * @return void + */ + public static function check_is_writable(string $dir):void { + + if (is_dir($dir) === false) { + throw new MDFileDoesNotExist("Directory " . $dir . " does not exist"); + } + + if (is_writable($dir) === false) { + throw new MDFileIsNotWritable("Directory " . $dir . " is not writable"); + } + + } } diff --git a/src/MD_STD_IN.php b/src/MD_STD_IN.php index b9578b6..f00d9f6 100644 --- a/src/MD_STD_IN.php +++ b/src/MD_STD_IN.php @@ -317,4 +317,25 @@ final class MD_STD_IN { return $input; } + + /** + * Wrapper around move_uploaded_file that throws errors in case the upload failed + * for an identifiable reason. + * + * @param string $filename Name of the file to upload. + * @param string $destination Destination to move the file to. + * + * @return boolean + */ + public static function move_uploaded_file(string $filename, string $destination):bool { + + MD_STD::check_is_writable(dirname($destination)); + + if (!(\move_uploaded_file($filename, $destination))) { + return false; + } + + return true; + + } }