summaryrefslogtreecommitdiffstats
path: root/app/class
diff options
context:
space:
mode:
authorMalf Furious <m@lfurio.us>2018-09-19 15:30:44 -0400
committerMalf Furious <m@lfurio.us>2018-09-19 15:30:44 -0400
commit77cbdc84d952643163d7086f79a633e5837be76d (patch)
treebfca192d611740474c2674836b3e6ea8cf4367c0 /app/class
parentac291fbe6db4a099bc8281e31ace13dad0a42d79 (diff)
downloadscrott-77cbdc84d952643163d7086f79a633e5837be76d.tar.gz
scrott-77cbdc84d952643163d7086f79a633e5837be76d.zip
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 <input> 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.
Diffstat (limited to 'app/class')
-rw-r--r--app/class/globals.php15
1 files changed, 15 insertions, 0 deletions
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);
+}
+
?>