DEV

fuelphp 1.8 - Upload

core/config/upload.phpが設定ファイル。保存パスや、アップロード可能なファイルタイプや容量等を設定できる。これを、app/config/upload.phpにコピーして編集する。あるいは、Upload::process();時に、設定情報の配列を引数として渡すこともできる。設定ファイルでauto_processをtrueにしておくと、早いタイミングで勝手にUpload::processされるので、引数を使う場合は、falseにしないとダメだと思う。

viewのFormは下記のような感じで、enctypeの記載が必要。で、1つ以上のtype=“file”のinputが必要。そうでないとエラーになる。

<?php echo Form::open(array('enctype' => 'multipart/form-data'))?>

アップロードの順番は、下記のような感じ。 Upload::process(); //セットアップ的な感じ Upload::is_valid(); //バリデーションチェック Upload::save(); //ファイル保存 Upload::get_errors() //エラー取得

もし保存前後や、バリデーションチェック後に、何かをしたい場合は、Upload::registerが使える。下記のような感じにすると、保存するファイル名を変更できる。(basenameだけ変えるだけでいいのかもしれない。)

Upload::register('validate', function(&amp;$file){
$file['basename'] = $new_basename;
$file['name'] = $file['basename'] . '.' . $file['extension'];
});

mp4ファイルのみアップロードできるようにする例

・ファイルアップロードは必須とする。未選択の場合、formにエラーを表示させる。 ・mp4拡張子しか受け付けないようにする。 ・アップロード時に、ファイル名、ファイルの説明、ファイルサイズ、ファイルパス等をmoviesテーブルに保存する。

app/config/upload.phpで拡張子を制限する。

return array(
/**
* global configuration
*/
// if true, the $_FILES array will be processed when the class is loaded
'auto_process' => false,
/**
* 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('mp4','mpeg4'),
// 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' => '',
// replace the extension of the uploaded file by this extension
'extension' => '',
// default path the uploaded files will be saved to
'path' => DOCROOT.'assets/movie',
// create the path if it doesn't exist
'create_path' => true,
// permissions to be set on the path after creation
'path_chmod' => 0757,
// 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' => true,
// 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,
);

モデルにFieldset作成関するを作る

public static function fieldset()
{
//大カテゴリー取得
$big_categories = Model_Category::get_big_category_list();
$fset = Fieldset::forge();
$fset->add('file', '動画ファイル', array('type' => 'file'));
$fset->add('title', 'タイトル')->add_rule('required');
$fset->add('description', '説明', array('type' => 'textarea'))->add_rule('required');
$fset->add('big_category_id', '大カテゴリー', array('type' => 'select', 'options' => $big_categories));
$fset->add('category_id', '小カテゴリー', array('type' => 'select'))->add_rule('required');
$fset->add('order_no', '表示順')->add_rule('required');
return $fset;
}

viewにFormを表示させる

<h1>動画登録</h1>
<?php echo Form::open(array('enctype' => 'multipart/form-data'))?>
<?php echo $fset->field('file')->set_attribute('class', 'form-control')->build()?>
<?php echo $fset->field('title')->set_attribute('class', 'form-control')->build()?>
<?php echo $fset->field('description')->set_attribute('class', 'form-control')->build()?>
<?php echo $fset->field('big_category_id')
->set_attribute('class', 'form-control')
->set_attribute('onchange', 'get_small_cat("' . Uri::create('rest/admin/categories/small_cat.json') . '");')
->build()?>
<?php echo $fset->field('category_id')->set_attribute('class', 'form-control')->build()?>
<button type="submit" class="btn btn-primary">登録</button>
<?php echo Html::anchor('admin/movies/', 'キャンセル', array('class' => 'btn btn-default'))?>
<?php echo Form::close()?>