From d53303e617334679bbc4bc164e6ab9c900714795 Mon Sep 17 00:00:00 2001 From: Joshua Ramon Enslin Date: Tue, 20 Jul 2021 02:04:55 +0200 Subject: [PATCH] Add option to pass acceptable mime types to MD_STD_IN::move_uploaded_file --- src/MD_STD.php | 6 +++--- src/MD_STD_IN.php | 11 ++++++++--- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/MD_STD.php b/src/MD_STD.php index 219edde..c6fc24a 100644 --- a/src/MD_STD.php +++ b/src/MD_STD.php @@ -675,7 +675,7 @@ final class MD_STD { */ public static function string_to_color_code(string $str):string { - $code = substr(dechex(crc32($str)), 0, 6); + $code = \substr(\dechex(\crc32($str)), 0, 6); return $code; } @@ -689,11 +689,11 @@ final class MD_STD { */ public static function check_is_writable(string $dir):void { - if (is_dir($dir) === false) { + if (\is_dir($dir) === false) { throw new MDFileDoesNotExist("Directory " . $dir . " does not exist"); } - if (is_writable($dir) === false) { + 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 f00d9f6..1497d12 100644 --- a/src/MD_STD_IN.php +++ b/src/MD_STD_IN.php @@ -322,13 +322,18 @@ final class MD_STD_IN { * 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. + * @param string $filename Name of the file to upload. + * @param string $destination Destination to move the file to. + * @param array $mime_types Optional array of acceptable mime types. If this is + * not empty, the file will be checked for having one + * of the given mime types. If it does not, an error + * will be thrown. * * @return boolean */ - public static function move_uploaded_file(string $filename, string $destination):bool { + public static function move_uploaded_file(string $filename, string $destination, array $mime_types = []):bool { + MD_STD::ensure_file($filename, $mime_types); MD_STD::check_is_writable(dirname($destination)); if (!(\move_uploaded_file($filename, $destination))) {