diff options
author | Malf Furious <m@lfurio.us> | 2016-03-31 21:51:29 -0400 |
---|---|---|
committer | Malf Furious <m@lfurio.us> | 2016-03-31 21:51:29 -0400 |
commit | d6d56ec5b3d46598d520ac77dd7e2fd4819e9361 (patch) | |
tree | d6ee85131fe0132f04570cbf9e2080e8345913e6 /app/class/form.class.php | |
parent | 4d69a5ecca13bbda5843c176e1bc4df514f079d7 (diff) | |
parent | 8ffc128d193929d9197af705974862b92a85a0fb (diff) | |
download | scrott-d6d56ec5b3d46598d520ac77dd7e2fd4819e9361.tar.gz scrott-d6d56ec5b3d46598d520ac77dd7e2fd4819e9361.zip |
Merge branch 'feature/file-uploads' into dev
Diffstat (limited to '')
-rw-r--r-- | app/class/form.class.php | 61 |
1 files changed, 60 insertions, 1 deletions
diff --git a/app/class/form.class.php b/app/class/form.class.php index 9f103ba..3f28a36 100644 --- a/app/class/form.class.php +++ b/app/class/form.class.php @@ -13,6 +13,7 @@ class Form $this->textFields = array(); $this->numbFields = array(); $this->enumFields = array(); + $this->fileFields = array(); $this->errorlist = array(); } @@ -86,9 +87,25 @@ class Form } /* + * Add new file field to the form + */ + function field_file($name, $maxsize, $allowed_mime = null, $req = false) + { + if ($req !== true) + $req = false; + + $this->fileFields[] = array( + 'name' => $name, + 'maxsize' => $maxsize, + 'mime' => $allowed_mime, + 'req' => $req + ); + } + + /* * Populate the form with input data from web page */ - function populate($input) + function populate($input, $files = null) { /* detect duplicate names */ $names = array(); @@ -98,6 +115,8 @@ class Form $names[] = $fld['name']; foreach ($this->enumFields as $fld) $names[] = $fld['name']; + foreach ($this->fileFields as $fld) + $names[] = $fld['name']; if (count(array_unique($names)) != count($names)) { @@ -178,9 +197,49 @@ class Form $this->logError($fld['name'] . " is required"); } + /* init file fields */ + foreach ($this->fileFields as $fld) + { + if (!is_null($files) && isset($files[$fld['name']])) + { + $file = $files[$fld['name']]; + + if ($file['error'] > 0) + { + $this->logError("An unknown error occurred"); + continue; + } + + if ($file['size'] > $fld['maxsize']) + { + $this->logError("File must be no larger than " . $fld['maxsize'] . " bytes"); + continue; + } + + if (is_array($fld['mime']) && array_search($file['type'], $fld['mime']) === false) + { + $this->logError("File type is not supported"); + continue; + } + + $this->$fld['name'] = $file; + } + + else if ($fld['req']) + $this->logError($fld['name'] . " is required"); + } + /* return */ return count($this->errorlist) == 0; } + + /* + * Save file uploaded through web form + */ + function saveUploadedFile($file, $filename) + { + return move_uploaded_file($file['tmp_name'], $filename); + } } ?> |