/** * Upload a file * * @param int $folder_id The folder to upload it to * @param bool $name The filename * @param string $field Like CI this defaults to "userfile" * @param bool $width The width to resize the image to * @param bool $height The height to resize the image to * @param bool $ratio Keep the aspect ratio or not? * @param string $alt "alt" attribute, here so that it may be set when photos are initially uploaded * @param array $allowed types * @return array|bool */ public static function upload($folder_id, $name = false, $field = 'userfile', $width = false, $height = false, $ratio = false, $allowed_types = false, $alt = NULL, $replace_file = false) { if (!($check_dir = self::check_dir(self::$path))) { return $check_dir; } if (!($check_cache_dir = self::check_dir(self::$_cache_path))) { return $check_cache_dir; } if (!($check_ext = self::_check_ext($field))) { return $check_ext; } // this keeps a long running upload from stalling the site session_write_close(); $folder = ci()->file_folders_m->get($folder_id); if ($folder) { ci()->load->library('upload'); $upload_config = array('upload_path' => self::$path, 'file_name' => $replace_file ? $replace_file->filename : self::$_filename, 'encrypt_name' => config_item('files:encrypt_filename') && !$replace_file ? TRUE : FALSE); // If we don't have allowed types set, we'll set it to the // current file's type. $upload_config['allowed_types'] = $allowed_types ? $allowed_types : self::$_ext; ci()->upload->initialize($upload_config); if (ci()->upload->do_upload($field)) { $file = ci()->upload->data(); $data = array('folder_id' => (int) $folder_id, 'user_id' => (int) ci()->current_user->id, 'type' => self::$_type, 'name' => $replace_file ? $replace_file->name : $name ? $name : $file['orig_name'], 'path' => '{{ url:site }}files/large/' . $file['file_name'], 'description' => $replace_file ? $replace_file->description : '', 'alt_attribute' => trim($replace_file ? $replace_file->alt_attribute : $alt), 'filename' => $file['file_name'], 'extension' => $file['file_ext'], 'mimetype' => $file['file_type'], 'filesize' => $file['file_size'], 'width' => (int) $file['image_width'], 'height' => (int) $file['image_height'], 'date_added' => now()); // perhaps they want to resize it a bit as they upload if ($file['is_image'] and ($width or $height)) { ci()->load->library('image_lib'); $config['image_library'] = 'gd2'; $config['source_image'] = self::$path . $data['filename']; $config['new_image'] = self::$path . $data['filename']; $config['maintain_ratio'] = (bool) $ratio; $config['width'] = $width ? $width : 0; $config['height'] = $height ? $height : 0; ci()->image_lib->initialize($config); ci()->image_lib->resize(); $data['width'] = ci()->image_lib->width; $data['height'] = ci()->image_lib->height; } if ($replace_file) { $file_id = $replace_file->id; ci()->file_m->update($replace_file->id, $data); } else { $data['id'] = substr(md5(microtime() . $data['filename']), 0, 15); $i = 0; while (ci()->file_m->exists($data['id'])) { $data['id'] = substr(md5(microtime() . $data['filename'] . $i++), 0, 15); } $file_id = $data['id']; ci()->file_m->insert($data); } if ($data['type'] !== 'i') { // so it wasn't an image. Now that we know the id we need to set the path as a download ci()->file_m->update($file_id, array('path' => '{{ url:site }}files/download/' . $file_id)); } if ($folder->location !== 'local') { header("Connection: close"); return Files::move($file_id, $data['filename'], 'local', $folder->location, $folder->remote_container); } header("Connection: close"); return self::result(true, lang('files:file_uploaded'), $data['name'], array('id' => $file_id) + $data); } else { $errors = ci()->upload->display_errors(); header("Connection: close"); return self::result(false, $errors); } } else { header("Connection: close"); return self::result(false, lang('files:specify_valid_folder')); } }
/** * Upload a file * * @param int $folder_id The folder to upload it to * @param bool $name The filename * @param string $field Like CI this defaults to "userfile" * @param bool $width The width to resize the image to * @param bool $height The height to resize the image to * @param bool $ratio Keep the aspect ratio or not? * @param string $alt "alt" attribute, here so that it may be set when photos are initially uploaded * @param array $allowed types * @return array|bool */ public function upload($folder_id, $name = false, $field = 'userfile', $width = false, $height = false, $ratio = false, $allowed_types = false, $alt = NULL, $replace_file = false) { if (!($check_dir = $this->checkDir($this->path))) { return $check_dir; } if (!($check_cache_dir = $this->checkDir($this->_cache_path))) { return $check_cache_dir; } if (!($check_ext = $this->_checkExt($field))) { return $check_ext; } // this keeps a long running upload from stalling the site session_write_close(); $folder = Folder::find($folder_id); if ($folder) { list($image_width, $image_height) = getimagesize($field); $file_size = $field->getClientSize(); $file_mimetype = $field->getMimeType(); $file_extension = $field->getClientOriginalExtension(); $file_original_name = str_replace(array($file_extension, '.'), '', $field->getClientOriginalName()); $file_name = $replace_file ? $replace_file->filename : $this->_filename; $full_file_name = $file_name . '.' . $file_extension; if ($field->move($this->path, $full_file_name)) { $user = Session::get('user'); $data = array('folder_id' => $folder_id, 'user_id' => $user['id'], 'type' => $this->_type, 'name' => $file_original_name, 'path' => '{site}/uploads/large/' . $file_name, 'description' => $replace_file ? $replace_file->description : '', 'alt_attribute' => trim($replace_file ? $replace_file->alt_attribute : $alt), 'filename' => $replace_file ? $replace_file->name : $name ? $name : $file_name, 'extension' => $file_extension, 'mimetype' => $file_mimetype, 'filesize' => $file_size, 'width' => $image_width, 'height' => $image_height); // create $file = File::create($data); $file->sort = $file->id; $file_id = $file->id; $file->save(); // resizing an uploaded file if ($this->_type == 'i') { Image::make($this->path . '/' . $full_file_name)->fit(75, 50)->save($this->thumbPath . '/' . $full_file_name); } if ($folder->location !== 'local') { header("Connection: close"); return Files::move($file_id, $data['filename'], 'local', $folder->location, $folder->remote_container); } header("Connection: close"); return $this->result(true, trans('files.file_uploaded'), $data['name'], array('id' => $file_id) + $data); } else { header("Connection: close"); return $this->result(false, trans('files.upload_error')); } } else { header("Connection: close"); return $this->result(false, trans('files.specify_valid_folder')); } }
/** * Upload a file * * @param int $folder_id The folder to upload it to * @param bool $name The filename * @param string $field Like CI this defaults to "userfile" * @param bool $width The width to resize the image to * @param bool $height The height to resize the image to * @param bool $ratio Keep the aspect ratio or not? * @return array|bool */ public static function upload($folder_id, $name = FALSE, $field = 'userfile', $width = FALSE, $height = FALSE, $ratio = FALSE) { if (!($check_dir = self::check_dir(self::$path))) { return $check_dir; } if (!($check_cache_dir = self::check_dir(self::$_cache_path))) { return $check_cache_dir; } if (!($check_ext = self::_check_ext($field))) { return $check_ext; } // this keeps a long running upload from stalling the site session_write_close(); $folder = ci()->file_folders_m->get($folder_id); if ($folder) { ci()->load->library('upload', array('upload_path' => self::$path, 'allowed_types' => self::$_ext, 'file_name' => self::$_filename)); if (ci()->upload->do_upload($field)) { $file = ci()->upload->data(); $data = array('folder_id' => (int) $folder_id, 'user_id' => (int) ci()->current_user->id, 'type' => self::$_type, 'name' => $name ? $name : $file['file_name'], 'path' => '{{ url:site }}files/large/' . $file['file_name'], 'description' => '', 'filename' => $file['file_name'], 'extension' => $file['file_ext'], 'mimetype' => $file['file_type'], 'filesize' => $file['file_size'], 'width' => (int) $file['image_width'], 'height' => (int) $file['image_height'], 'date_added' => now()); // perhaps they want to resize it a bit as they upload if ($file['is_image'] and $width or $height) { ci()->load->library('image_lib'); $config['image_library'] = 'gd2'; $config['source_image'] = self::$path . $data['filename']; $config['new_image'] = self::$path . $data['filename']; $config['maintain_ratio'] = $ratio; $config['width'] = $width; $config['height'] = $height; ci()->image_lib->initialize($config); ci()->image_lib->resize(); ci()->image_lib->clear(); } $file_id = ci()->file_m->insert($data); if ($data['type'] !== 'i') { // so it wasn't an image. Now that we know the id we need to set the path as a download ci()->file_m->update($file_id, array('path' => '{{ url:site }}files/download/' . $file_id)); } if ($folder->location !== 'local') { return Files::move($file_id, $data['filename'], 'local', $folder->location, $folder->remote_container); } return self::result(TRUE, lang('files:file_uploaded'), $data['name'], array('id' => $file_id) + $data); } else { $errors = ci()->upload->display_errors(); return self::result(FALSE, $errors); } } else { return self::result(FALSE, lang('files:specify_valid_folder')); } }
/** * Rename a file * * @param int $id The id of the file * @param string $name The new name * @return array * **/ public static function rename_file($id = 0, $name) { $data = array('name' => $name); $file = ci()->file_m->select('files.*, file_folders.location')->join('file_folders', 'file_folders.id = files.folder_id')->get_by('files.id', $id); // if it's a local file we can rename the actual file if ($file and $file->location === 'local') { Files::move($id, $name); } else { ci()->file_m->update($id, $data); } return self::result(TRUE, lang('files:item_updated'), $name, $data); }