Updating A Model Linked To A File


I have a model named CategoriesImages. When i created an item, i upload a file, and stores it on the server. In this case, i use a CUploadedFile, with

$model->image = CUploadedFile::getInstance($model, 'image');

But, when i update this item, i don’t want to re-upload the file. If there is no uploaded file in the form, i’d like to generate a file object from the file on the server, so that the user won’t have to re-upload an existing file if he doesn’t want to modify the “file” property of the model.

Any clue ?


If I understand you correctly, you can just check whether CUploadedFile::getInstance returns null, meaning that no file was uploaded. In this case, you don’t update the attribute of your model.

Also, please check the wiki How to upload a file using a model. You can improve your code following it.