【问题标题】:Cannot upload file using CUploadedFile in Yii 1.1.x无法在 Yii 1.1.x 中使用 CUploadedFile 上传文件
【发布时间】:2014-02-14 09:46:21
【问题描述】:

我正在尝试使用 Yii 中的 CUploadedFile 功能上传图像。我的模型中有以下函数 - 错误发生在 beforeSave() 中。显然,当我检查目录时文件不存在时由于某种原因没有保存文件。

这是我收到的错误:

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

我的模型代码如下:

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();
}

当我在 beforeSave() 中 var_dump $display_image 时,我得到以下对象

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

谁能帮助我解释为什么我在 beforeSave() 函数中出现此错误

【问题讨论】:

    标签: php file-upload yii frameworks


    【解决方案1】:

    由于路径不正确,问题出在 $display_image 变量中:

            //$display_image = param('productImagePath').$filename;//$this->Guid.'.'.$this->new_display_image->extensionName;
    
            $display_image = './shopimages/'.$filename;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-18
      • 2015-12-09
      • 1970-01-01
      相关资源
      最近更新 更多