Upload file in yii framework is easy than the others. Simple to use this, you can use the functions of saveAs in yii framework. Ok let’s suppose you have a table like bellow:
CREATE TABLE document
(
id
int(11) NOT NULL AUTO_INCREMENT,
doc_name
varchar(255) DEFAULT NULL,
doc_file
varchar(255) NOT NULL,
doc_type
varchar(50) DEFAULT NULL,
doc_size
varchar(50) DEFAULT NULL,
summary
text,
up_dated
date DEFAULT '0000-00-00',
PRIMARY KEY (id
)
)
You can use gii to generate the model and the CRUD form and then if you have already have those files. And now you can go to your form to edit the form like this. Don’t forget to add the htmlOptions as multipart form data like below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
<div class="form"> <?php $form = $this->beginWidget('CActiveForm', array( 'id' => 'document-form', 'enableAjaxValidation' => false, 'htmlOptions' => array('enctype' => 'multipart/form-data') )); ?> <?php Yii::app()->clientScript->registerScript('search', " $('.search-button').click(function(){ $('.doc-form').toggle(); return false; }); $('.doc-form form').submit(function(){ $.fn.yiiGridView.update('document-grid', { data: $(this).serialize() }); return false; }); "); ?> <p class="note">Fields with <span class="required">*</span> are required.</p> <?php echo $form->errorSummary($model); ?> <div class="row"> <?php echo $form->labelEx($model, 'doc_name'); ?> <?php echo $form->textField($model, 'doc_name', array('size' => 50, 'maxlength' => 255)); ?> <?php echo $form->error($model, 'doc_name'); ?> </div> <div class="row"> <?php echo $form->labelEx($model, 'doc_file'); ?> <?php echo $form->fileField($model, 'doc_file', array('size' => 36, 'maxlength' => 255)); ?> <?php echo $form->error($model, 'doc_file'); ?> </div> <div class="row"> <?php echo $form->labelEx($model, 'summary'); ?> <?php echo $form->textArea($model, 'summary', array('rows' => 6, 'cols' => 50)); ?> <?php echo $form->error($model, 'summary'); ?> </div> <div class="row buttons"> <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?> </div> <?php $this->endWidget(); ?> </div> |
>
And then in your DocumentController just update the actionUpdate and actionCreate like bellow
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
public function actionCreate() { $model = new Document; $model->up_dated = date('Y-m-d'); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); $path = Yii::app()->basePath . '/../uploads'; if (!is_dir($path)) { mkdir($path); } if (isset($_POST['Document'])) { $model->attributes = $_POST['Document']; if (@!empty($_FILES['Document']['name']['doc_file'])) { $model->doc_file = $_POST['Document']['doc_file']; if ($model->validate(array('doc_file'))) { $model->doc_file = CUploadedFile::getInstance($model, 'doc_file'); } else { $model->doc_file = ''; } $model->doc_file->saveAs($path . '/' . time() . '_' . str_replace(' ', '_', strtolower($model->doc_file))); $model->doc_type = $model->doc_file->getType(); $model->doc_size = $model->doc_file->getSize(); } $model->doc_file = time() . '_' . str_replace(' ', '_', strtolower($model->doc_file)); if ($model->save()) { $this->redirect(array('view', 'id' => $model->id)); } } $this->render('create', array( 'model' => $model, )); } |
Oky that’s so simple to create an upload for yii framework. If you want to make some filter of your files use the rules in your model, in this case is Document model like this..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public function rules() { // NOTE: you should only define rules for those attributes that // will receive user inputs. return array( array('doc_name', 'required'), array('doc_name', 'length', 'max' => 255), array('doc_file', 'file', 'types' => 'jpg, gif, png, pdf, doc, docx, odt, txt, xlsx, xls, csv, zip', 'allowEmpty' => true, 'maxSize' => 1024 * 1024 * 50, 'tooLarge' => 'The file was larger than 50MB. Please upload a smaller file.'), array('doc_type, doc_size', 'length', 'max' => 50), array('summary, up_dated, doc_file', 'safe'), // The following rule is used by search(). // Please remove those attributes that should not be searched. array('id, doc_name, doc_type, doc_size, summary, up_dated, doc_file', 'safe', 'on' => 'search'), ); } |
That’s it, Have a nice day.