我已经在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/