Ejemplo n.º 1
0
 /**
  * Try to Upload the given file returning the filename on success
  *
  * @param array $file $_FILES array element
  * @param string $dir destination directory
  * @param boolean $overwrite existing files of the same name?
  * @param integer $size maximum size allowed (can also be set in php.ini or server config)
  */
 public function file($file, $dir, $overwrite = FALSE, $size = FALSE)
 {
     // Invalid upload?
     if (!isset($file['tmp_name'], $file['name'], $file['error'], $file['size']) or $file['error'] != UPLOAD_ERR_OK) {
         return FALSE;
     }
     // File to large?
     if ($size and $size > $file['size']) {
         return FALSE;
     }
     // Create $basename, $filename, $dirname, & $extension variables
     extract(pathinfo($file['name']) + array('extension' => ''));
     // Make the name file system safe
     $filename = sanitize_filename($filename);
     // We must have a valid name and file type
     if (empty($filename) or empty($extension)) {
         return FALSE;
     }
     $extension = strtolower($extension);
     // Don't allow just any file!
     if (!$this->allowed_file($extension)) {
         return FALSE;
     }
     // Make sure we can use the destination directory
     Directory::usable($dir);
     // Create a unique name if we don't want files overwritten
     $name = $overwrite ? "{$filename}.{$ext}" : $this->unique_filename($dir, $filename, $extension);
     // Move the file to the correct location
     if (move_uploaded_file($file['tmp_name'], $dir . $name)) {
         return $name;
     }
 }