twitter-bootstrap - Yii 中的图片上传

标签 twitter-bootstrap yii

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

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


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

最佳答案

试试这段代码。

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

希望对你有帮助。

谢谢

关于twitter-bootstrap - Yii 中的图片上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22033799/

相关文章:

html - 单击图像右侧时防止 Bootstrap 轮播滑动

php - 用于具有 NULL 值的复合键的表的关系 Yii ActiveRecord

javascript - Bootstrap 4 提前输入

html - Bootstrap 中的响应式交错行

javascript - Twitter Bootstrap 3 模态中的 jQuery Datepicker

php - 在 Yii 2.0 (Menu::Widget) 中使用跨度?

mysql - 在 Yii1 中使用变量获取原始 SQL 查询

php - 如何在CDetailView中添加宽度标签以在Yii中显示图像

php - Yii 应用程序与用户负载

javascript - 当我以 Angular 使用进度条时 CPU 高