Changeset 2648

Show
Ignore:
Timestamp:
05/06/08 21:47:22 (2 months ago)
Author:
Shadowhand
Message:

Making upload::save() more flexible and concise, fixing a bug in upload::valid() (stupid types)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/system/helpers/upload.php

    r2647 r2648  
    1616         * Save an uploaded file to a new location. 
    1717         * 
    18          * @param   string   name of $_FILE input 
     18         * @param   mixed    name of $_FILE input or array of upload data 
    1919         * @param   string   new filename 
    2020         * @param   string   new directory 
    2121         * @return  string   full path to new file 
    2222         */ 
    23         public function save($name, $filename = NULL, $directory = NULL) 
     23        public function save($file, $filename = NULL, $directory = NULL) 
    2424        { 
     25                // Load file data from FILES if not passed as array 
     26                $file = is_array($file) ? $file : $_FILES[$file]; 
     27 
    2528                if ($filename === NULL) 
    2629                { 
    2730                        // Use the default filename, with a timestamp pre-pended 
    28                         $filename = time().$_FILES[$name]['name']; 
     31                        $filename = time().$file['name']; 
    2932                } 
    3033 
     
    4750                        throw new Kohana_Exception('upload.not_writable', $directory); 
    4851 
    49                 if (is_uploaded_file($_FILES[$name]['tmp_name']) AND move_uploaded_file($_FILES[$name]['tmp_name'], $filename = $directory.$filename)) 
     52                if (is_uploaded_file($file['tmp_name']) AND move_uploaded_file($file['tmp_name'], $filename = $directory.$filename)) 
    5053                { 
    5154                        // Move the file to the upload directory 
     
    8083         * @return  bool 
    8184         */ 
    82         public static function valid($file) 
     85        public static function valid(array $file) 
    8386        { 
    8487                return (isset($file['tmp_name']) 
    8588                        AND isset($file['error']) 
    8689                        AND is_uploaded_file($file['tmp_name']) 
    87                         AND $file['error'] === UPLOAD_ERR_OK); 
     90                        AND (int) $file['error'] === UPLOAD_ERR_OK); 
    8891        } 
    8992