From db369507f636f9395604a5cd72fcfe5d99f97166 Mon Sep 17 00:00:00 2001 From: Malf Furious Date: Wed, 27 Apr 2016 22:11:58 -0400 Subject: Add function Form::saveFile() This is a rework of how the framework handles performing file uploads. Rather than attaching new fields to a form (of type file) and handling them during populate() then saving later, users can call what is essentially a static function and save files in isolation. Since each webform I can conceive using in Scrott at this time won't be uploading more than one file at a time, this model should work nicely moving forward; however can be easily adjusted if need be. --- app/class/form.class.php | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'app') diff --git a/app/class/form.class.php b/app/class/form.class.php index 9f103ba..907c0a2 100644 --- a/app/class/form.class.php +++ b/app/class/form.class.php @@ -181,6 +181,47 @@ class Form /* return */ return count($this->errorlist) == 0; } + + /* + * Handle an uploaded file + */ + function saveFile($file, $maxsize, $allowed_mime, $path, $req = false) + { + if (isset($file) && !is_null($file)) + { + if ($file['error'] > 0) + { + $this->logError("An unknown error occurred"); + return false; + } + + if ($file['size'] > $maxsize) + { + $this->logError("File must be no larger than " . $maxsize . " bytes"); + return false; + } + + if (is_array($allowed_mime) && array_search($file['type'], $allowed_mime) === false) + { + $this->logError("File type is not supported"); + return false; + } + + if (!move_uploaded_file($file['tmp_name'], $path)) + { + $this->logError("Error saving uploaded file"); + return false; + } + } + + else if ($req) + { + $this->logError("File upload is required"); + return false; + } + + return true; + } } ?> -- cgit v1.2.3