作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我无法在我的数据库和文件夹中上传照片...如果我使用 CModel 类来构建表单,同样的代码正在工作...但如果我使用纯 html,它会给我错误 Profilepic cannot be blank
...我的代码如下
我的表格
<form method="post" action="<?php echo Yii::app()->getBaseUrl(true).'/index.php?r=user/create'?>" enctype="multipart/form-data">
<div class="row">
<label>Username</label><input type="text" name="username"/><span><?php if(isset($error['username'])) echo $error['username'][0]; ?></span>
</div>
<div class="row">
<label>Password</label><input type="password" name="password"/><span><?php if(isset($error['password'])) echo $error['password'][0]; ?></span>
</div>
<div class="row">
<label>Email</label><input type="text" name="email"/><span><?php if(isset($error['email'])) echo $error['email'][0]; ?></span>
</div>
<div class="row">
<label>Profile pic</label><input type="file" name="profilepic"/><span><?php if(isset($error['profilepic'])) echo $error['profilepic'][0]; ?></span>
</div>
<input type="hidden" value="No" name="flag"/>
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
public function actionCreate()
{
$model=new User;
// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
if(isset($_POST))
{
$uploadedFile=CUploadedFile::getInstance($model,'profilepic');
$fileName = "{$uploadedFile}";
$model->profilepic = $fileName;
$model->attributes=$_POST;
if($model->save())
{
$a=$uploadedFile->saveAs(Yii::app()->basePath.'/../images/'.$fileName);
$this->redirect(array('create'));
}
}
$this->render('create',array(
'model'=>$model,
));
}
return array(
array('username, password,email,profilepic', 'required'),
array('email','email'),
array('profilepic','file','types'=>'jpg,jpeg,png'),
array('email','unique'),
array('username,password,email,flag,profilepic', 'safe', 'on'=>'search'),
);
最佳答案
您需要更改 Controller 代码,例如遵循...
public function actionCreate()
{
$model=new User;
if(Yii::app()->request->isPostRequest) //change it
{
$model->profilepic=CUploadedFile::getInstanceByName('profilepic');
// $fileName = "{$uploadedFile}"; //this is not required
// $model->profilepic = $fileName; //this is also not required
$model->attributes=$_POST;
if($model->save())
{
$model->profilepic->saveAs(Yii::app()->basePath.'/../images/'.$model->profilepic);
$this->redirect(array('create'));
}
}
$this->render('create',array(
'model'=>$model,
));
}
关于php - 如何在yii中上传照片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20215370/
我是一名优秀的程序员,十分优秀!