doufen3786 2014-02-14 09:46
浏览 35
已采纳

在Yii 1.1.x中无法使用CUploadedFile上传文件

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

  • 写回答

1条回答 默认 最新

  • doutangxi2144 2014-02-14 11:36
    关注

    The problem was in the $display_image var due to an incorrect path:

            //$display_image = param('productImagePath').$filename;//$this->Guid.'.'.$this->new_display_image->extensionName;
    
            $display_image = './shopimages/'.$filename;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序