Changes
Title
unchanged
How to upload a file using a model
Category
unchanged
Tutorials
Yii version
unchanged
Tags
changed
File upload
Content
changed
First declare an attribute to store the file name in the model class (either a form model or an active record model). Also declare a `file` validation rule for this attribute to ensure a file is uploaded with specific extension name.
```php
class Item extends CActiveRecord
{
public $image;
// ... other attributes
public function rules()
{
return array(
array('image', 'file', 'types'=>'jpg, gif, png'),
);
}
}
```
Then, in the controller class define an action method to render the form and collect user-submitted data.
```php
class ItemController extends CController
{
public function actionCreate()
{
$model=new Item;
if(isset($_POST['Item']))
{
$model->attributes=$_POST['Item'];
$model->image=CUploadedFile::getInstance($model,'image');
if($model->save())
{
$model->image->saveAs('path/to/localFile');
// redirect to success page
}
}
$this->render('create', array('model'=>$model));
}
}
```
Finally, create the action view and generate a file upload field.
```php
<?php echo CHtml::form('','post',array('enctype'=>'multipart/form-data')); ?>
...
<?php echo CHtml::activeFileField($model, 'image'); ?>
...
<?php echo CHtml::endForm(); ?>
```
### Links
[Russian Version](http://dbhelp.ru/form-file-upload/page/)
[Chinese version](http://projects.ourplanet.tk/node/93)## The Model
First declare an attribute to store the file name in the model class (either a form model or an active record model).
Also declare a `file` validation rule for this attribute to ensure a file is uploaded with specific extension name.
```php
class Item extends CActiveRecord
{
public $image;
// ... other attributes
public function rules()
{
return array(
array('image', 'file', 'types'=>'jpg, gif, png', 'safe' => false),
);
}
}
```
You can add others validation parameters as described in [CFileValidator].
For instance, one can add a "maxSize" restriction (the PHP ini settings will of course prevail).
## The Controller
Then, in the controller class define an action method to render the form and collect user-submitted data.
```php
class ItemController extends CController
{
public function actionCreate()
{
$model=new Item;
if(isset($_POST['Item']))
{
$model->attributes=$_POST['Item'];
$model->image=CUploadedFile::getInstance($model,'image');
if($model->save())
{
$model->image->saveAs('path/to/localFile');
// redirect to success page
}
}
$this->render('create', array('model'=>$model));
}
}
```
[CUploadedFile::saveAs()] in one of the methods of [CUploadedFile].
You can also access directly to the file through its "tempName" property.
## The View
Finally, create the action view and generate a file upload field.
```php
$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();
```
Another syntax is to use static calls in [CHtml] instead of [CActiveForm].
The result is the same as above.
```php
<?php echo CHtml::form('','post',array('enctype'=>'multipart/form-data')); ?>
...
<?php echo CHtml::activeFileField($model, 'image'); ?>
...
<?php echo CHtml::submitButton('Submit'); ?>
<?php echo CHtml::endForm(); ?>
```
### Links
* [Russian Version](http://dbhelp.ru/form-file-upload/page/)
* [Persian Version](http://shgn.ir/%d8%a2%d9%be%d9%84%d9%88%d8%af-%d9%81%d8%a7%db%8c%d9%84-%d8%af%d8%b1-yii/)
* [You can also check : How to upload image(photo), and path entry in datebase with update functionality](http://www.yiiframework.com/wiki/349/how-to-upload-image-photo-and-path-entry-in-datebase-with-update-functionality/ "How to upload image(photo), and path entry in datebase with update functionality")