diff options
| author | Malf Furious <m@lfurio.us> | 2016-04-27 22:26:21 -0400 | 
|---|---|---|
| committer | Malf Furious <m@lfurio.us> | 2016-04-27 22:26:21 -0400 | 
| commit | 93fa361ba63c8afa2370c43b7918c9dd513a4586 (patch) | |
| tree | 818451967b797a67ac74c3fe1b42afb6ee2f7141 /app/class | |
| parent | d6d56ec5b3d46598d520ac77dd7e2fd4819e9361 (diff) | |
| parent | db369507f636f9395604a5cd72fcfe5d99f97166 (diff) | |
| download | scrott-93fa361ba63c8afa2370c43b7918c9dd513a4586.tar.gz scrott-93fa361ba63c8afa2370c43b7918c9dd513a4586.zip | |
Merge branch 'bug/file-upload' into dev
Diffstat (limited to '')
| -rw-r--r-- | app/class/form.class.php | 88 | 
1 files changed, 35 insertions, 53 deletions
| diff --git a/app/class/form.class.php b/app/class/form.class.php index 3f28a36..907c0a2 100644 --- a/app/class/form.class.php +++ b/app/class/form.class.php @@ -13,7 +13,6 @@ class Form          $this->textFields = array();          $this->numbFields = array();          $this->enumFields = array(); -        $this->fileFields = array();          $this->errorlist  = array();      } @@ -87,25 +86,9 @@ 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, $files = null) +    function populate($input)      {          /* detect duplicate names */          $names = array(); @@ -115,8 +98,6 @@ 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))          { @@ -197,48 +178,49 @@ class Form                  $this->logError($fld['name'] . " is required");          } -        /* init file fields */ -        foreach ($this->fileFields as $fld) +        /* 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 (!is_null($files) && isset($files[$fld['name']])) +            if ($file['error'] > 0)              { -                $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; -                } +                $this->logError("An unknown error occurred"); +                return false; +            } -                if (is_array($fld['mime']) && array_search($file['type'], $fld['mime']) === false) -                { -                    $this->logError("File type is not supported"); -                    continue; -                } +            if ($file['size'] > $maxsize) +            { +                $this->logError("File must be no larger than " . $maxsize . " bytes"); +                return false; +            } -                $this->$fld['name'] = $file; +            if (is_array($allowed_mime) && array_search($file['type'], $allowed_mime) === false) +            { +                $this->logError("File type is not supported"); +                return false;              } -            else if ($fld['req']) -                $this->logError($fld['name'] . " is required"); +            if (!move_uploaded_file($file['tmp_name'], $path)) +            { +                $this->logError("Error saving uploaded file"); +                return false; +            }          } -        /* return */ -        return count($this->errorlist) == 0; -    } +        else if ($req) +        { +            $this->logError("File upload is required"); +            return false; +        } -    /* -     * Save file uploaded through web form -     */ -    function saveUploadedFile($file, $filename) -    { -        return move_uploaded_file($file['tmp_name'], $filename); +        return true;      }  } | 
