Upload and Save

I am using the standard file upload method as is available in URL here. I modified the code a little to save the file attribute in a table.

But in the controller, if I do

if ($model->upload()) {



the code doesn’t execute and gives an error.

But if I interchange upload() and save() it works.

if ($model->save()) {



What could be the reason?

First you should validate the model then save the Model and then save the file into the folder. The code should be like below

$imageFile = UploadedFile::getInstance($model, 'imageFile');

$model->image = $imageFile->baseName . '.' . $imageFile->extension;

if ($model->validate() && $model->save()) {

    $imageFile->saveAs('uploads/' . $imageFile->baseName . '.' . $imageFile->extension);

    // return success

} else {

    // return error


What I am trying to say is that save() before saveAs() works but saveAs() before save() doesn’t work.

The problem may be due to the fact that temporary file no more exists after saveAs(). The error message is:


finfo_file(/tmp/phpQKeAlT): failed to open stream: No such file or directory

But why should $model->save() look for the temporary file? I am not saving it in the database as a blob.