【问题标题】:Image upload in YiiYii 中的图片上传
【发布时间】:2014-02-26 06:45:12
【问题描述】:

我已经在 Yii 框架中实现了图片文件上传。它正在上传图像并成功存储到数据库中,但图像没有存储在我的目录中。在我的控制器中,我添加了以下代码:

public function actionCreate()
{
    $model=new Friends;

    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);

    if(isset($_POST['Friends']))
    {
        $model->attributes=$_POST['Friends'];

                 $image=CUploadedFile::getInstance($model,'profile_image');
             if (is_object($image) && get_class($image)==='CUploadedFile') 
                {
                  $model->profile_image=$image->name;    
                }
                if($model->save())
            if(is_object($image))
            $image->saveAs(dirname(__FILE__).'/../../friends_picture/'.$model->profile_image);   

        $this->redirect(array('view','id'=>$model->id));
    }

    $this->render('create',array(
        'model'=>$model,
    ));
}


错误在哪里?给定的目录名是否正确?

【问题讨论】:

  • dirname(FILE).'/../../friends_picture/'.$model->profile_image 回显这个语句,看看这是否是你想要的路径存储它或其产生错误的路径。

标签: twitter-bootstrap yii


【解决方案1】:

试试这个代码。

public function actionCreate()
{
    $model=new Friends;

    // Uncomment the following line if AJAX validation is needed
    // $this->performAjaxValidation($model);

    if(isset($_POST['Friends']))
    {
        $model->attributes=$_POST['Friends'];

        $model->profile_image = CUploadedFile::getInstance($model,'profile_image');

        if($model->save())

            $fullImgSource = Yii::getPathOfAlias('webroot').'/friends_picture/'.$model->profile_image;
            $model->profile_image->saveAs($fullImgSource);


        $this->redirect(array('view','id'=>$model->id));
    }

    $this->render('create',array(
        'model'=>$model,
    ));
}

愿它帮助你。

谢谢

【讨论】:

  • 更新代码。再次测试
【解决方案2】:

试试这个

$model->profile_image = CUploadedFile::getInstance($model,'profile_image');

    if($model->save())

        $fullImgSource = Yii::getPathOfAlias('webroot').'/friends_picture/'.$model->profile_image;
        $model->profile_image->saveAs($fullImgSource);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-29
    • 2017-10-30
    • 1970-01-01
    • 1970-01-01
    • 2015-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多