diff options
| author | Malf Furious <m@lfurio.us> | 2016-04-27 22:11:58 -0400 | 
|---|---|---|
| committer | Malf Furious <m@lfurio.us> | 2016-04-27 22:11:58 -0400 | 
| commit | db369507f636f9395604a5cd72fcfe5d99f97166 (patch) | |
| tree | 818451967b797a67ac74c3fe1b42afb6ee2f7141 | |
| parent | 9458874e8c194b1c5a53bd3e85f9ba7548c9dac4 (diff) | |
| download | scrott-db369507f636f9395604a5cd72fcfe5d99f97166.tar.gz scrott-db369507f636f9395604a5cd72fcfe5d99f97166.zip | |
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.
Diffstat (limited to '')
| -rw-r--r-- | app/class/form.class.php | 41 | 
1 files changed, 41 insertions, 0 deletions
| 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; +    }  }  ?> | 
