I am trying to upload a image using the CUploadedFile functionality in Yii. I have the following functions in my model - the error occurs in the beforeSave(). Obviously the file isn't saving for some reason when I check the directory no file is present.
This is error I am receiving:
move_uploaded_file(/var/www/myshop/public_html/shopimages/29a80cf8-630f-11e2-9998-14dae9d6777c.jpg)
[function.move-uploaded-file]: failed to open stream: No such file or directory
My model code is as follows:
public function beforeValidate()
{
$this->new_display_image = CUploadedFile::getInstance($this, 'new_display_image');
return parent::beforeValidate();
}
public function beforeSave()
{
if ($this->new_display_image && !$this->hasErrors()) {
$filename = (($this->isNewRecord && empty($this->Guid)) ? str_replace('.','-',uniqid('', true)) : $this->Guid).'.'.$this->new_display_image->extensionName;
$display_image = param('productImagePath').$filename;//$this->Guid.'.'.$this->new_display_image->extensionName;
if ($this->new_display_image->saveAs($display_image))
$this->display_image = basename($display_image);
}
}
public function afterSave()
{
if($this->isNewRecord)
{
extract($this->getAttributes());
$lastSavedObject = self::model()->findByAttributes(compact('product_name','colour_id','organisation_id'));
$lastSavedObject->colour_id = 21;
//Uploaded image if there is any, and replace with GUID.
if(!empty($display_image))
{
$baseUploadDirectory = param('productImagePath');
$display_image = $baseUploadDirectory . $display_image;
$correct_display_image = $baseUploadDirectory . $lastSavedObject->Guid.".".pathinfo($display_image, PATHINFO_EXTENSION);
//For any reason; if product image is removed, misplaced
if(file_exists($display_image))
{
if(rename($display_image, $correct_display_image))
$lastSavedObject->display_image = basename($correct_display_image);
}else{
$lastSavedObject->display_image = 'placeholder.jpg';
}
}else{
$lastSavedObject->display_image = 'placeholder.jpg';
}
$lastSavedObject->save();
$this->Guid = $lastSavedObject->Guid;
}
return parent::afterSave();
}
When I var_dump $display_image in the beforeSave() I get the following object
object(CUploadedFile)[281]
private '_name' => string '200.jpg' (length=7)
private '_tempName' => string 'C:\wamp\tmp\php9207.tmp' (length=23)
private '_type' => string 'image/jpeg' (length=10)
private '_size' => int 13959
private '_error' => int 0
private '_e' (CComponent) => null
private '_m' (CComponent) => null
Can anyone assist me in why I am getting this error within the beforeSave() function