In fact, Yii has a file upload class. For verification, you can use the model rules:
array('url_img', 'file','types'=>'jpg, gif, png', 'allowEmpty'=>true, 'wrongType'=>Yii::t('app','bad_file')),
You can upload files using ajax, for example:
var fd = new FormData();
fd.append( "ModelName[image]", $("#your_input_id")[0].files[0]);
$.ajax({
url: url,
type: 'POST',
cache: false,
data: fd,
dataType: "json",
processData: false,
contentType: false,
success: function (data) {
}
});
To save the file, follow these steps:
$model=new ModelName;
CUploadedFile::getInstance($model,"image");
if ($model->validate()){
$model->image->saveAs("path/to/save/image.png");
}
http://www.yiiframework.com/wiki/2/how-to-upload-a-file-using-a-model/
- :
$form = $this->beginWidget(
'CActiveForm',
array(
'id' => 'upload-form',
'enableAjaxValidation' => false,
'htmlOptions' => array('enctype' => 'multipart/form-data'),
)
);
echo $form->labelEx($model, 'image');
echo $form->fileField($model, 'image');
echo $form->error($model, 'image');
echo CHtml::submitButton('Submit');
$this->endWidget();