- Requirements
- Installation
- Usage
- Sample Controller Action
- Note
- For any Questions or Suggestions feel free to ask me!
ruploadbehavior helps the developer to manage file upload operation and other related file operations such as uploading files with unique name, delete previous file when when new file is uploaded, delete file together with parent record automatically
Requirements ¶
Yii 1.1.14
Installation ¶
place this extension under protected/extensions folder and import it to configuration's import array like this
'import' => array(
'ext.ruploadbehavior.*'
),
Usage ¶
attach the behavior provided with this extension into your model as
public function behaviors()
{
return array(
'upload' => array(
'class' => 'UploadBehavior',
//file column name
'field' => 'file',
// file upload path, path must not end with slash ( / )
'path' => 'assets',
// upload unique files
'renameIfExists' => true,
// delete previous file when new file uploaded
'delete_previous_file' => true,
// delete file when parent record is deleted
'delete_together' => true
)
);
}
Sample Controller Action ¶
You can use Gii generated actions without any modifications
public function actionCreate()
{
$model=new Product;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST['Product']))
{
$model->attributes=$_POST['Product'];
if($model->save())
$this->redirect(array('view','id'=>$model->id));
}
$this->render('create',array(
'model'=>$model,
));
}
Note ¶
don't forget to use file field for file upload and the form must use enctype multipart/form-data
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.