Publicado en Oct 11, 2014
Veamos como subir ficheros en Yii framework.
El controlador:
1 | public function actionIndex(){ |
3 | $form = new UsuariosForm(); |
5 | if (isset( $_FILES [ "UsuariosForm" ])){ |
8 | $file =CUploadedFile::getInstance( $form , "fichero" ); |
11 | $name =time(). "-" . $file ->getName(); |
15 | if ( $file ->getExtensionName()== "png" || $file ->getExtensionName()== "pdf" || $file ->getExtensionName()== "zip" ){ |
18 | $file ->saveAs(Yii::getPathOfAlias( "webroot" ). "/uploads/" . $name ); |
22 | Yii::app()->user->setFlash( 'mensaje' , 'Solo ficheros png, pdf o zip' ); |
28 | $this ->render( 'index' , array ( |
La vista:
1 | <h1>Subir ficheros con Yii</h1> |
2 | <?php echo CHtml::beginForm( '' , 'post' , array ( "name" => "form" , "id" => "form" , "enctype" => "multipart/form-data" ));?> |
3 | Fichero ( Png, pdf o zip ) |
5 | echo CHtml::activeFileField( $form , "fichero" ); |
8 | <?php echo CHtml::submitButton( 'submit' , array ( "class" => "btn btn-success" , "value" => "Subir" , "title" => "Subir" )); ?> |
9 | <?php if (Yii::app()->user->hasFlash( 'mensaje' )){ |
12 | <span class = "alert alert-danger" ><?php echo Yii::app()->user->getFlash( 'mensaje' )?></span> |
14 | <?php echo CHtml::endForm();?> |
El mensaje de error flash solo se muestra una vez.

Más información:
http://www.yiiframework.com/wiki/2/how-to-upload-a-file-using-a-model/
http://www.yiiframework.com/wiki/21/how-to-work-with-flash-messages/