示例#1
0
 public function save()
 {
     if (count($this->file_array) > 0) {
         $this->log('Capturing input %s', $this->input);
         if (array_key_exists($this->input, $this->file_array)) {
             // set original filename if not have a new name
             if (empty($this->filename)) {
                 $this->log('Using original filename %s', $this->file_array[$this->input]['name']);
                 $this->filename = $this->file_array[$this->input]['name'];
             }
             // 扩展名
             //$extension = preg_replace(
             //"/^[\p{L}\d\s\-\_\.\(\)]*\.([\d\w]+)$/iu",
             //'$1',
             //$this->file_array[$this->input]["name"]
             //);
             $extension = Tools_help::getFileExt($this->file_array[$this->input]['name']);
             $this->filename = sprintf($this->filename, $extension);
             // set file info
             $this->file['mime'] = $this->file_array[$this->input]['type'];
             $this->file['tmp'] = $this->file_array[$this->input]['tmp_name'];
             $this->file['original'] = $this->file_array[$this->input]['name'];
             $this->file['size'] = $this->file_array[$this->input]['size'];
             $this->file['sizeFormated'] = Tools_help::sizeFormat($this->file['size']);
             $this->file['destination'] = $this->destination_directory . $this->filename;
             $this->file['filename'] = $this->filename;
             $this->file['error'] = $this->file_array[$this->input]['error'];
             // Check if exists file
             if ($this->fileExists($this->destination_directory . $this->filename)) {
                 $this->log('%s file already exists', $this->filename);
                 // Check if overwrite file
                 if ($this->overwrite_file === false) {
                     $this->log('You don\'t allow overwriting. Show more about FileUpload::allowOverwriting');
                     return false;
                 }
                 $this->log('The %s file is overwritten', $this->filename);
             }
             // Execute input callback
             if (!empty($this->callbacks['input'])) {
                 $this->log('Running input callback');
                 call_user_func($this->callbacks['input'], (object) $this->file);
             }
             // Check mime type
             $this->log("Check mime type");
             if (!$this->checkMimeType($this->file['mime'])) {
                 $this->log('Mime type %s not allowed', $this->file['mime']);
                 return false;
             }
             $this->log('Mime type %s allowed', $this->file['mime']);
             // Check file size
             if ($this->max_file_size > 0) {
                 $this->log('Checking file size');
                 if ($this->max_file_size < $this->file["size"]) {
                     $this->log('The file exceeds the maximum size allowed(Max: %s; File: %s)', Tools_help::sizeFormat($this->max_file_size), Tools_help::sizeFormat($this->file["size"]));
                     return false;
                 }
             }
             // Copy tmp file to destination and change status
             $this->log('Copy tmp file to destination %s', $this->destination_directory);
             $this->log('Using upload function: %s', $this->upload_function);
             $this->file['status'] = call_user_func_array($this->upload_function, array($this->file_array[$this->input]['tmp_name'], $this->destination_directory . $this->filename));
             // Execute output callback
             if (!empty($this->callbacks['output'])) {
                 $this->log('Running output callback');
                 call_user_func($this->callbacks['output'], (object) $this->file);
             }
             return $this->file['status'];
         }
     }
 }