public static function setup_upload_file() { // prepare upload file. $original_file = FBD_BASEPATH . 'data/development/test/media/img/sample_01.jpg'; $upload_file = APPPATH . 'tmp/sample.jpg'; \Util_file::copy($original_file, $upload_file); chmod($upload_file, 0777); return $upload_file; }
public static function save_from_file_path($file_path, $save_name = '', $is_image = true) { if (!($bin = Util_file::get_encoded_bin_data($file_path, $is_image))) { throw new FuelException('Binary data is invalid.'); } $obj = self::forge(); $filepath = Util_File::get_filepath_from_path($file_path); if (!$save_name) { $save_name = Site_Upload::convert_filepath2filename($filepath); } $obj->name = $save_name; $obj->bin = $bin; return $obj->save(); }
public function check_real_file_tmp_info($file_path) { $file_info = File::file_info($file_path); $file_name = $file_info['basename']; $file = Model_FileTmp::get4name($file_name); // file に対応する Model_File が存在する $this->assertNotEmpty($file); $is_thumbnail = Util_file::get_path_partial($file_info['dirname'], 1) == 'thumbnail'; // path の確認 $length = $is_thumbnail ? 3 : 2; $offset = $is_thumbnail ? 1 : 0; $this->assertEquals(trim($file->path, '/'), Util_file::get_path_partial($file_info['dirname'], $length, $offset)); // type の確認 if ($file_info['mimetype'] != 'application/zip') { $this->assertEquals($file->type, $file_info['mimetype']); } // size の確認 if (!$is_thumbnail) { $this->assertEquals($file->filesize, $file_info['size']); } }
public static function move_files_to_tmp_dir($moved_files) { foreach ($moved_files as $moved_file) { Util_file::move($moved_file['to'], $moved_file['from']); } }
public static function clear_exif($path, $driver = null) { if (!$driver) { $driver = Config::get('image.driver'); } if (FBD_IMAGE_DRIVER == 'imagemagick') { $command = FBD_IMAGE_IMGMAGICK_PATH . 'convert'; $params = sprintf('"%s" -strip "%s"', $path, $path); Util_Toolkit::exec_command($command, $params, false, true); } elseif (FBD_IMAGE_DRIVER == 'imagick') { $im = new \Imagick($path); $im->stripImage(); $im->writeImage($path); $im->destroy(); } else { Util_file::resave($path); } return File::get_size($path); }
private function make_image($original_file_path, $target_file_dir, $target_filename) { $target_file_path = $target_file_dir . $target_filename; Site_Upload::check_and_make_uploaded_dir($target_file_dir); Util_file::resize($original_file_path, $target_file_path, $this->width, $this->height, $this->resize_type); }
private static function execute_clean_file($file_type, $is_tmp = false) { if (!self::$absolute_execute && !\Site_Util::check_is_dev_env()) { throw new \FuelException('This task is not work at prod env.'); } $raw_file_dir_path = \Site_Upload::get_uploaded_path('raw', $file_type, $is_tmp); $cache_file_dir_path_key = 'upload.types.' . $file_type; if ($is_tmp) { $cache_file_dir_path_key .= '.tmp'; } $cache_file_dir_path_key .= '.root_path.cache_dir'; $cache_file_dir_path = DOCROOT . conf($cache_file_dir_path_key); if (!file_exists($raw_file_dir_path) && ($cache_file_dir_path && !file_exists($cache_file_dir_path))) { return "File directry '" . $raw_file_dir_path . "' not exists."; } $file_paths = \Util_file::get_file_recursive($raw_file_dir_path); if ($cache_file_dir_path) { $file_paths = array_merge($file_paths, \Util_file::get_file_recursive($cache_file_dir_path)); } if (!$file_paths) { return sprintf("No files at '%s'", $raw_file_dir_path); } $i = 0; foreach ($file_paths as $file_path) { if (self::check_file_exists_record($file_path, $is_tmp)) { continue; } \Util_file::remove($file_path); $i++; } $subject = $file_type; if ($is_tmp) { $subject .= '_tmp'; } return sprintf('%d %s removed.', $i, $subject); }
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; }
private function set_file_properties($file_path) { $ext = Util_file::get_image_type($file_path); $file_info = File::file_info($file_path); $this->file->size = $file_info['size']; $this->file->type = $file_info['mimetype']; if (empty($this->file->original_name)) { $this->file->original_name = $file_info['filename']; } if (!($this->file->name = \Site_Upload::make_unique_filename($ext, $this->options['filename_prefix'], $this->file->original_name, $this->options['upload_dir']))) { throw new FuelException('File already exists.'); } $this->file->file_path = $this->options['upload_dir'] . str_replace($this->options['filename_prefix'], '', $this->file->name); }