Hello!
I try create files uploading.
the problem is that I cant pass validation when I use getInstance[color="#FF0000"]s[/color]
this is my controller
public function actionIndex()
{
$model = new ImageUpload();
$dataProvider=new CActiveDataProvider('BannerModel');
if(isset($_POST['upload']))
{
$dir = Yii::getPathOfAlias('webroot').'/images/banners/';
$model->attributes = $_POST['upload'];
$files = CUploadedFile::getInstances($model, 'file');
// if($model->validate())
// {
foreach($files as $file)
{
if(file_exists(Yii::app()->request->getBaseUrl(true).'/images/banners/'.$file->getName()))
{
Yii::app()->user->setFlash('error', "Error!");
$this->render('index',array('dataProvider'=>$dataProvider,'model'=>$model));
exit();
}
else
{
$sql = "INSERT INTO banner (`id`, `imgsrc`, `status`) VALUES (NULL, '".$file->getName()."', '1')";
Yii::app()->db->createCommand($sql)->execute();
$file->saveAs($dir.'/'.$file->getName());
Yii::app()->user->setFlash('success', "OK!");
$this->render('index',array('dataProvider'=>$dataProvider,'model'=>$model));
}
}
}
else
{
$this->render('index',array('dataProvider'=>$dataProvider,'model'=>$model));
}
}
and this is my model
class ImageUpload extends CFormModel
{
public $file;
public function rules()
{
return array(
array('file', 'file', 'types'=>'jpg, jpeg, png', 'maxSize' => 10485760), // 10 mb
);
}
}