Пример #1
0
 private function check_file_name()
 {
     if (empty($this->file_name)) {
         return false;
     }
     $ext = Util_file::get_extension_from_filename($this->file_name);
     $accept_formats = Site_Upload::get_accept_format($this->type);
     if (!$accept_formats || !in_array($ext, $accept_formats)) {
         return false;
     }
     $this->extension = $ext;
     return true;
 }
Пример #2
0
 public static function check_file_type($file_path, $arrow_extentions = array(), $format = '', $upload_type = 'img')
 {
     if (empty($arrow_extentions)) {
         $arrow_extentions = Site_Upload::get_accept_format($upload_type);
     }
     if ($format) {
         if (!($extension = Site_Upload::check_file_format_is_accepted($format, $upload_type))) {
             return false;
         }
     } else {
         if (!($extension = self::check_extension($file_path, $arrow_extentions))) {
             return false;
         }
     }
     if ($upload_type == 'file') {
         return $extension;
     }
     $imginfo = getimagesize($file_path);
     $type = $imginfo[2];
     switch ($type) {
         case IMAGETYPE_JPEG:
             if ($extension == 'jpg' || $extension == 'jpeg') {
                 return 'jpg';
             }
             break;
         case IMAGETYPE_GIF:
             if ($extension == 'gif') {
                 return 'gif';
             }
             break;
         case IMAGETYPE_PNG:
             if ($extension == 'png') {
                 return 'png';
             }
             break;
     }
     return false;
 }
Пример #3
0
 protected function handle_file_upload($uploaded_file, $original_name, $size, $type, $error, $index = null, $content_range = null)
 {
     $file = new \stdClass();
     $file->is_tmp = $this->options['is_tmp'];
     $file->original_name = $original_name;
     $file->size = $this->fix_integer_overflow(intval($size));
     $file->type = $type;
     if (!($extention = Util_file::check_file_type($uploaded_file, \Site_Upload::get_accept_format($this->options['upload_type']), $type, $this->options['upload_type']))) {
         $file->error = $this->get_error_message('accept_file_types');
         return $file;
     }
     if (!($filename_with_prefix = Site_Upload::make_unique_filename($extention, $this->options['filename_prefix'], $original_name))) {
         $file->error = 'ファイル名の作成に失敗しました。';
         return $file;
     }
     $file->name = $this->remove_filename_prefix($filename_with_prefix);
     $file->name_prefix = $this->options['filename_prefix'];
     if (!\Site_Upload::check_and_make_uploaded_dir($this->options['upload_dir'], null, $this->options['mkdir_mode'])) {
         $file->error = 'ディレクトリの作成に失敗しました。';
         return $file;
     }
     if (!$this->validate($uploaded_file, $file, $error, $index)) {
         return $file;
     }
     if ($this->options['upload_type'] == 'img') {
         $file->thumbnail_uri = $this->options['image_versions']['thumbnail']['upload_url'] . $file->name;
     }
     $this->handle_form_data($file, $index);
     $upload_dir = $this->get_upload_path();
     $file_path = $this->get_upload_path($file->name);
     $append_file = $content_range && is_file($file_path) && $file->size > $this->get_file_size($file_path);
     if ($uploaded_file && is_uploaded_file($uploaded_file)) {
         // multipart/formdata uploads (POST method uploads)
         if ($append_file) {
             file_put_contents($file_path, fopen($uploaded_file, 'r'), FILE_APPEND);
         } else {
             $res = move_uploaded_file($uploaded_file, $file_path);
         }
     } else {
         // Non-multipart uploads (PUT method support)
         file_put_contents($file_path, fopen('php://input', 'r'), $append_file ? FILE_APPEND : 0);
     }
     $file_size = $this->get_file_size($file_path, $append_file);
     if ($file_size === $file->size) {
         $file->url = $this->get_download_url($file->name);
         if ($this->is_valid_image_file($file_path)) {
             $this->handle_image_file($file_path, $file);
         }
     } else {
         $file->size = $file_size;
         if (!$content_range && $this->options['discard_aborted_uploads']) {
             $this->delete_file($filename_with_prefix, $this->options['storage_type']);
             $file->error = 'abort';
         }
     }
     $this->set_additional_file_properties($file);
     // exif データの取得
     $exif = array();
     if ($this->options['is_save_exif_to_db'] && $extention == 'jpg') {
         $exif = \Util_Exif::get_exif($file_path, $this->options['exif_accept_tags'], $this->options['exif_ignore_tags']);
     }
     if ($this->options['upload_type'] == 'img') {
         // 大きすぎる場合はリサイズ & 保存ファイルから exif 情報削除
         $file_size_before = $file->size;
         if ($this->options['member_id'] && $this->options['user_type'] === 0 && ($max_size = Site_Upload::get_accepted_max_size($this->options['member_id']))) {
             $file->size = Site_Upload::check_max_size_and_resize($file_path, $max_size);
         }
         // Exif情報の削除
         $is_resaved = $file->size != $file_size_before;
         if ($this->options['is_clear_exif_on_file'] && !$is_resaved) {
             Site_Upload::clear_exif($file_path);
             $file->size = File::get_size($file_path);
         }
         if (!empty($this->options['accept_sizes'])) {
             $file->accept_sizes = $this->options['accept_sizes'];
         }
     }
     try {
         if ($this->options['storage_type'] != 'normal') {
             $this->save_file2storage($file_path, $filename_with_prefix);
             $this->delete_file($filename_with_prefix, $this->options['storage_type'], false, false);
         }
         $file->id = $this->save_file($file, $exif);
     } catch (\Exception $e) {
         if ($this->options['is_output_log_save_error']) {
             \Util_Toolkit::log_error(sprintf('file save error: %s', $e->getMessage()));
         }
         $this->delete_file($filename_with_prefix, $this->options['storage_type']);
         $file->error = 'ファイルの保存に失敗しました。';
     }
     return $file;
 }