Fuelphpでのファイルアップロード

fuelphpでファイルをアップロードするとき、なかなか参考資料がなかったのでメモ。 基本的にはリファレンスを解読すれば一つ一つの関数について書いてあるので理解は出来るのですが、 サンプルがなかなか無く、もっと現物的なものが欲しかったので。 結構fuelphpが気に入ったので、記事はしばらくfuelphp関連になるかもしれません。

設定ファイル

  1. まず、fuel/core/config/upload.phpをfuel/app/configフォルダにコピー
  2. それぞれのプロジェクトによってupload.phpを書き換える 自分が今回した設定は次の通り。変えたのはファイル名が被らないようにsuffixを追加したのと、upload場所をappやcoreフォルダと並べたuploadフォルダにしたこと。ちなみにuploadフォルダは自動で作られる設定にしたので自分では作っていない。
 return array( /** * global configuration */ // if true, the $_FILES array will be processed when the class is loaded 'auto_process' => true, /** * file validation settings */ // maximum size of the uploaded file in bytes. 0 = no maximum 'max_size' => 0, // list of file extensions that a user is allowed to upload 'ext_whitelist' => array(), // list of file extensions that a user is NOT allowed to upload 'ext_blacklist' => array(), // list of file types that a user is allowed to upload // ( type is the part of the mime-type, before the slash ) 'type_whitelist' => array(), // list of file types that a user is NOT allowed to upload 'type_blacklist' => array(), // list of file mime-types that a user is allowed to upload 'mime_whitelist' => array(), // list of file mime-types that a user is NOT allowed to upload 'mime_blacklist' => array(), /** * file save settings */ // prefix given to every file when saved 'prefix' => '', // suffix given to every file when saved 'suffix' => '-' . time(), // replace the extension of the uploaded file by this extension 'extension' => '', // default path the uploaded files will be saved to 'path' => APPPATH . '../upload' , // create the path if it doesn't exist 'create_path' => true, // permissions to be set on the path after creation 'path_chmod' => 0777, // permissions to be set on the uploaded file after being saved 'file_chmod' => 0666, // if true, add a number suffix to the file if the file already exists 'auto_rename' => true, // if true, overwrite the file if it already exists (only if auto_rename = false) 'overwrite' => false, // if true, generate a random filename for the file being saved 'randomize' => false, // if true, normalize the filename (convert to ASCII, replace spaces by underscores) 'normalize' => false, // valid values are 'upper', 'lower', and false. case will be changed after all other transformations 'change_case' => false, // maximum lengh of the filename, after all name modifications have been made. 0 = no maximum 'max_length' => 0 ); 

アップロード

実際のactionで書いたのはこちら

 class Controller_Upload extends Controller public function action_upload(){ if (Input::method() == 'POST'){ FuelCoreUpload::process(); if (count(FuelCoreUpload::get_files()) == 1 && FuelCoreUpload::is_valid()){ FuelCoreUpload::save(0); $file = FuelCoreUpload::get_files(0); /** * ここで$fileを使ってアップロード後の処理 * $fileの中にいろんな情報が入っている **/ Response::redirect('upload/finished'); }else{ Session::set_flash('error',$this->getFileErrorMessages()); } } $this->template->content = View::forge('upload/upload'); } private function getFileErrorMessages(){ $errorMessage = "<ul>"; foreach (FuelCoreUpload::get_errors() as $file) { foreach ($file['errors'] as $error) { $errorMessage .= "<li>{$error['message']}</li>"; } } $errorMessage .= '</ul>'; return $errorMessage; } } 

やっていることは

  1. POSTであることを確認
  2. アップされたファイルの処理(※まだ保存しない)
  3. ファイルの条件の確認(数や大きさ、拡張子など)
  4. ファイルの保存

です。 viewはそれぞれがんばってください。 注意点としては、きちんとformタグのところだけは

<?php echo Form::open(array('class' => 'form-stacked','enctype'=>"multipart/form-data")); ?> 

とenctypeをつけてあげる事ぐらいです。

img_show