2016-06-02 14:28:55 3566次浏览 1条回答 1 悬赏 10 金钱

我想做一个用户头像上传预览的功能代码
视图:

<?php  $form = ActiveForm::begin(['enableAjaxValidation' => false,'action' => ['user/regist'],'options' => ['enctype' => 'multipart/form-data']]); ?>
		  	<?= $form->field($model, 'username')->textInput(['class'=>'menk form-control menk-vtcl']) ?> 
			<?= $form->field($model, 'password')->passwordInput(['class'=>'menk form-control menk-vtcl']) ?> 
			<?= $form->field($model, 'password_compare')->passwordInput(['class'=>'menk form-control menk-vtcl']) ?> 
			<?= $form->field($model, 'email')->textInput(['class'=>'menk form-control menk-vtcl']) ?>  
			<?= $form->field($model, 'sex')->radioList(['男'=>'ᠡᠷᠡᠭᠲᠡᠢ','女'=>'ᠡᠮᠡᠭᠲᠡᠢ']) ?>
			<?= $form->field($model, 'num')->textInput(['class'=>'menk form-control menk-vtcl'])?>
			<?= $form->field($model, 'img')->fileInput(['class'=>'menk form-control menk-vtcl']) ?>
			 			
			<?= Html::submitButton('ᠳᠡᠪᠰᠢᠭᠦᠯᠬᠦ',['class'=>'menk btn btn-primary-outline menk-vtcl']) ?>
			<?= Html::resetButton('ᠲᠠᠬᠢᠨ  ᠪᠢᠴᠢᠬᠦ',['class'=>'menk btn btn-primary-outline menk-vtcl']) ?>
		<?php $form=ActiveForm::end();?> 

模型中:

 public function rules()
    {
    	return [
    		[['username','img'],'required','message'=>'ᠬᠣᠭᠣᠰᠡᠨ ᠪᠠᠶᠢᠵᠦ ᠪᠣᠯᠬᠣᠷ ᠦᠭᠡᠢ'],
    		['username','filter','filter'=>'trim'],
    		['username','unique','targetClass'=>'\app\models\user\User','message'=>' ᠡᠨᠡ ᠨᠡᠷ᠎ᠡ ᠲᠠᠩᠰᠠᠯᠵᠦ ᠦᠩᠭᠡᠷᠰᠡᠨ᠂ ᠦᠪᠡᠷ ᠨᠡᠷ᠎ᠡ ᠠᠪᠠᠭᠠᠷᠠᠢ︕'],
    		//密码比较
    		[['password','password_compare'],'required','message'=>' ᠨᠣᠮᠧᠷ ᠬᠣᠭᠣᠰᠡᠨ ᠪᠠᠶᠢᠵᠦ ᠪᠣᠯᠬᠤ  ᠦᠭᠡᠢ'],
    		[['password'],'match','pattern'=>'/^[A-Za-z0-9]{4,99}$/','message'=>' ᠨᠣᠮᠧᠷ ᠠᠩᠭᠢᠯᠢ ᠶᠡᠬᠡ ᠪᠠᠭ᠎ᠠ᠂ ᠲᠣᠭ᠎ᠠ᠂ 4  ᠪᠣᠯᠥᠨ99 ᠣᠷᠣᠨ ᠲᠠᠢ ᠲᠣᠭ᠎ᠠ'],
    		['password_compare','compare','compareAttribute'=>'password','message'=>' ᠬᠣᠶᠠᠷ ᠨᠣᠮᠧᠷ ᠡᠷᠬᠡᠪᠴᠢ ᠨᠢᠭᠡᠲᠦᠯᠭᠡ ᠲᠠᠢ ᠪᠠᠶᠢᠬᠤ ᠬᠡᠷᠡᠭᠲᠡᠢ'],
    		['email', 'email','message'=> 'ᠬᠠᠶᠠᠭ ᠲᠠᠭᠠᠷᠰᠡᠨ ᠦᠭᠡᠢ︕'],
    		[['sex'],'required','message'=> 'ᠬᠣᠭᠣᠭᠰᠠᠨ ᠪᠠᠶᠢᠵᠦ ᠦᠭᠡᠢ︕'],
    		[['num'],'match','pattern'=>'/^13(\d{9})$|^15(\d{9})$|^189(\d{8})$/','message'=> 'ᠭᠠᠷ  ᠣᠲᠠᠰᠣ ᠶᠢᠨ ᠵᠦᠪ ᠪᠢᠴᠢᠭᠡᠷᠡᠢ'],    		
    		[['img'], 'file'],
    	]; 	
    }
    public function signup()
    {
    	
    	if ($this->validate()) {
    		echo '1212';
    		exit();
    		$user = new User();
    		$user->username = $this->username;
    		$user->setPassword($this->password);
    		$user->email=$this->email;
    		$user->num=$this->num;
    		$user->sex=$this->sex;
    		$user->create_time=gmdate('Y-m-d H:m:s',time()+8*3600);	   		
    		//$user->img=saveAs('uploads/' . $this->img->baseName . '.' . $this->img->extension); 
    		//$user->img=$this->img;
    		if ($user->save()) {
    			$roles=new U_R();
    			$save=$roles->save_roles($user->id);
    			if(!empty($save)){
    			return $user;
    			}else{
    				echo '用户没有保存等级';
    				exit();
    			}
    		}
    	}
    	return null;
    }

控制器:

public function actionRegist(){	
			$model = $this->RegistForm();
			$model->load($_POST);
	        if (Yii::$app->request->isAjax) {
	            Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
	            return \yii\bootstrap\ActiveForm::validate($model);
	        }	        
	        if (Yii::$app->request->post()) {	     	        	        	   
	        	$model->img = UploadedFile::getInstances($model, 'img');
	        	//$ext = getExtension($model->img);
	        	//$imageName = time().rand(100,999).'.'.$ext;
	        	//$image->saveAs('uploads/' . $model->img->baseName . '.' . $model->img->extension); //设置图片的存储位置
	        	//$model->img = 'http://www.xxx.cn/uploads/'.$imageName;//设置图片的url,可以用_SERVER["HTTP_HOST"]+图片名代替		        	
	        	var_dump($model->img) ;//string(10) "ad_sys.png" NULL 
	        	exit();	      
	        		if($user = $model->signup()){
	        			//$user->img=$model->img->saveAs('uploads/' . $model->img->baseName . '.' . $model->img->extension);
	        			if(Yii::$app->getUser()->login($user)){
	        				return $this->redirect(['user/index']);
	        			}
	        		}	        			        	   	
	        }		
			return $this->render('regist', ['model' => $model,]);	
		}

就一直保存不上,测试了很多种,很多遍,谁有更好的方法或者代码可以分享一下呀??

  • 回答于 2016-06-03 09:29 举报

    saveAs的uploads在哪个文件夹啊?应该用Yii::$app->basePath作为网站根目录拼接上后续的你写的那些,前面还要加一个判断文件夹是否存在,不存在则创建的操作,不然不行

    1 条回复
    回复于 2016-06-03 10:00 回复

    uploads在web文件夹下面呢,$image->saveAs(Yii::$app->basePath.'web/uploads/' . $model->img->baseName . '.' . $model->img->extension); 这样呢?

您需要登录后才可以回答。登录 | 立即注册
SaSa
助理

SaSa

注册时间:2016-03-28
最后登录:2017-10-15
在线时长:11小时23分
  • 粉丝9
  • 金钱55
  • 威望0
  • 积分165

热门问题