7
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

FuelPHPでアップロードファイルをバリデーション

Last updated at Posted at 2014-12-25

バリデーションルール

ダウンロード(Gist)

fuel/app/classes/validation/asset.php
<?php

/**
 * 独自バリデーションを追加
 */
class Validation_Asset
{
	/**
	 * アップロードファイルをチェック
	 */
	public static function _validation_upload($val, $config = null)
	{
		Upload::process($config);
			
		if( Upload::is_valid() ){
			Upload::save();
			return true;
		}
		
		return false;
		//return Upload::get_error(); //TODO send error message
	}
}

コンフィグ

  • upload.phpとuploads.phpがあるのでややこしい

ダウンロード(Gist)

fuel/app/config/uploads.php
<?php
return array(
	'photo1' => array(
		'path'             => APPPATH. 'tmp/image/',
		'max_size'         => 2 * 1024 * 1024,
		'ext_whitelist'    => array('jpg','jpeg','gif','png'),
	),
);

ダウンロード(Gist)

fuel/app/config/upload.php
<?php
return array(
	'auto_process'     => false,
	'auto_rename'      => false,
	'overwrite'        => true,
	'randomize'        => true,
);

バリデーション

fuel/app/classes/model/assets.php

	/*
	 * 画像アップロード時のバリデーション
	 */
	public static function validate($factory)
	{
		$val = Validation::forge($factory);
		$val->add_callable('Validation_Asset');
		
		Config::load('uploads');
		$photo_config = Config::get('photo1');

		$val->add('photo', 'Photo')
			->add_rule('upload', $photo_config )
		;
		
		return $val;
	}
7
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?