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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 根据以下文字信息,做EA模型图
  • ¥15 删除虚拟显示器驱动 删除所有 Xorg 配置文件 删除显示器缓存文件 重启系统 可是依旧无法退出虚拟显示器
  • ¥15 vscode程序一直报同样的错,如何解决?
  • ¥15 关于使用unity中遇到的问题
  • ¥15 开放世界如何写线性关卡的用例(类似原神)
  • ¥15 关于并联谐振电磁感应加热
  • ¥60 请查询全国几个煤炭大省近十年的煤炭铁路及公路的货物周转量
  • ¥15 请帮我看看我这道c语言题到底漏了哪种情况吧!
  • ¥60 关机时蓝屏并显示KMODE_EXCEPTION_NOT_HANDLED,怎么修?
  • ¥66 如何制作支付宝扫码跳转到发红包界面