From 77cbdc84d952643163d7086f79a633e5837be76d Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Wed, 19 Sep 2018 15:30:44 -0400 Subject: globals: Add function saveIfFile() This is an alternative function to globals' saveFile(), which allows model code to just pass in the name of the expected uploaded file, rather than requiring them to look up the file themselves. This is in line with my preference to encapsulate PHP superglobals access away from most of the codebase. Note that even if the user opts not to upload optional files, the associated file field will still be present in $_FILES, with a special error code set (meaning 'no file uploaded') which setFile() ignores. It is only in the case of a malformed form submission that $_FILES will be missing the requested file field, prompting Scrott to throw an exception. --- app/class/globals.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'app') diff --git a/app/class/globals.php b/app/class/globals.php index 8ac67e1..6e3b7fd 100644 --- a/app/class/globals.php +++ b/app/class/globals.php @@ -247,4 +247,19 @@ function saveFile(array $file, string $path, int $maxsize, ?array $allowedMime = return true; } +/* + * Similar to saveFile, but takes the uploaded file field name, + * rather than the array directly. The file is looked up in + * $_FILES. If it does not exist, an exception is thrown. + */ +function saveIfFile(string $file, string $path, int $maxsize, ?array $allowedMime = NULL, + ?string &$origName = NULL, ?string &$origMime = NULL) : bool +{ + if (!isset($_FILES[$file])) + throw new Exception("Requested file upload, but no data was supplied"); + + $f = $_FILES[$file]; + return saveFile($f, $path, $maxsize, $allowedMime, $origName, $origMime); +} + ?> -- cgit v1.2.3