It is possible that I already have answered this here within the last update after your comments but I'll add a different answer here as it is one more different issue beside the others. This error is thrown when using $file_instance->baseName
or $file_instance->extension
after saving the file. saveAs() whish its content is the following :
public function saveAs($file, $deleteTempFile = true)
{
if ($this->error == UPLOAD_ERR_OK) {
if ($deleteTempFile) {
return move_uploaded_file($this->tempName, $file);
} elseif (is_uploaded_file($this->tempName)) {
return copy($this->tempName, $file);
}
}
return false;
}
has a boolean $deleteTempFile
argument set to true by default. which means it will delete the temporary file after saving and you will not be able to save the uploaded file again in the same request. You can set it to false if needed by doing :
$file_instance->saveAs('...',false)
BUT it won't be a good practice in this case as you are getting 2 copies of the same file. If the file is already saved to its new path then why not deleting it from the tmp
folder. Just hold that path in a variable before calling saveAs() and use it when needed like it is done in the answer linked above :
$path = 'uploads/room_img/' . $file_instance->baseName . '.' . $file_instance->extension;
$file_instance->saveAs($path);
$model->images = $path;
Other issues you need to solve are :
foreach ($model->file as $file_instance)
{
$model->save();
$model->images = $path;
}
The $model->save()
here have no logic to me. You have a single instance of an object and foreach file instance stored in its attribute $model->file
you are saving the hole model again and again. And I understand from the second one ($model->images = $path
) that your model have an attribute images
to which you can assigning a string value. but you have multiple images here where each of them has its own path and inside the loop you are overriding it each time with the next image path string. If is the case I would probably store the path to folder where I can find all those images later instead :
$model->images = 'uploads/room_img/[a_collection_id_or_wathever_unique]/'
And inside that folder I would host the related images. Or maybe I'll host all paths separated by ;
or equivalent by doing this inside the loop :
$model->images .= $path . ';';
In case if images
are related models represented by an ActiveRecord class then the answer linked above should fix it.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…