Uploading a file

Ok Gang:

I’ve been searching, and reading, and searching to find information on uploading a file that would meet my need. And nothing seems to fit. Can any one help?


  • Create and update actions may or may not have a file uploaded.

  • When a file is uploaded, I need to rename the saved file as well as save that name in the ‘file_name’ column.

  • I also want to save the the size and mimeType into the ‘file_size’ and ‘file_type’ columns respectively.

  • If the $model->save() works, THEN move/save the file.

  • If, during update the file is being changed, I need to overwrite/delete the old file and save the new one with the old filename.

  • MVC seems to dictate that this should be down in the beforeSave()/afterSave() functions in the model, but all of the code examples use the controller.

I am just so confused :huh: that I don’t know where to look anymore.

Any help would be greatly appreciated. JK

Anybody? Please…

There’s an extension for uploading files: http://www.yiiframework.com/extension/sfancyupload/

It might be overkill, though. Honestly, I’m a Yii noob so I don’t know exactly what functions and classes Yii has to handle file uploads, but if it were me I would just write the code in manually and use the beforeSave method in the model to handle the renaming and all that stuff. I know for a fact that the beforeSave function can definitely be used to handle this - though you can use the controller as well I suppose, e.g.:

public function actionCreate()




        //add code here