当前位置:首页 > CMS教程 > 其它CMS > 列表

YII2框架实现表单中上传单个文件的方法示例

发布:smiling 来源: PHP粉丝网  添加日期:2022-02-20 09:15:31 浏览: 评论:0 

本文实例讲述了YII2框架实现表单中上传单个文件的方法,分享给大家供大家参考,具体如下:

有些时候我们提交的表单中含有文件,怎么样让表单里的数据和文件一起提交。

我的数据表tb_user内容如下:

  1. CREATE TABLE `tb_user` ( 
  2.  `id` int(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '用户ID'
  3.  `namevarchar(32) DEFAULT '' COMMENT '用户名'
  4.  `pwd` varchar(64) DEFAULT '' COMMENT '密码'
  5.  `head_img` varchar(256) DEFAULT '' COMMENT '图像'
  6.  `sex` tinyint(1) DEFAULT '0' COMMENT '性别(0:男,1:女)'
  7.  `age` tinyint(3) DEFAULT '0' COMMENT '年龄'
  8.  PRIMARY KEY (`id`) 
  9. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户表'

表单页面代码如下(至于为什么没有用ActiveForm来创建,这个就不解释了):

  1. <?php 
  2. use yii\helpers\Url; 
  3. ?> 
  4. <!doctype html> 
  5. <html lang="zh-CN"> 
  6. <head> 
  7.   <meta charset="UTF-8"> 
  8.   <title>表单提交</title> 
  9. </head> 
  10. <body> 
  11. <form action="<?php echo Url::toRoute('index/index'); ?>" method="post" enctype="multipart/form-data"> 
  12.   姓名:<input type="text" name="name"><br> 
  13.   密码:<input type="password" name="pwd"><br> 
  14.   性别:<input type="radio" name="sex" value="0" checked>男 
  15.      <input type="radio" name="sex" value="1"><br> 
  16.   年龄:<input type="number" name="age"><br> 
  17.   头像:<input type="file" name="head_img"><br> 
  18.   <input type="submit" value="提交"> 
  19.   <input name="_csrf" type="hidden" value="<?php echo \Yii::$app->request->csrfToken; ?>"> 
  20. </form> 
  21. </body> 
  22. </html> 

模型类代码如下:

  1. <?php 
  2.  
  3. namespace app\models; 
  4.  
  5. use yii\db\ActiveRecord; 
  6. use yii\web\UploadedFile; 
  7.  
  8. class MyUser extends ActiveRecord 
  9.   //注意这里的上传路径是相对你入口文件 
  10.   const UPLOAD_PAHT = 'uploads/'
  11.  
  12.   //返回你要操作的数据表名 
  13.   public static function tableName() 
  14.   { 
  15.     return '{{%user}}'
  16.   } 
  17.  
  18.   //设置规则,验证表单数据 
  19.   public function rules() 
  20.   { 
  21.     return [ 
  22.       ['name''required''message' => '请填写用户名'], 
  23.       ['pwd''string''length' => [6, 12], 'message' => '密码6-12位'], 
  24.       ['sex''in''range' => [0, 1], 'message' => '正确选择性别'], 
  25.       ['age''integer''min' => 1, 'max' => 120, 'message' => '正确填写年龄'], 
  26.       ['head_img''image''extensions' => ['png''jpg''gif'], 'maxSize' => 1024 * 1024 * 1024, 'message' => '请上传头像'], 
  27.     ]; 
  28.   } 
  29.  
  30.   //上传头像 
  31.   public function uploadHeadImg() 
  32.   { 
  33.     //'head_img'这个字符串必须跟你表单中file控件的name字段相同 
  34.     $head_img = UploadedFile::getInstanceByName('head_img'); 
  35.     if (!emptyempty($head_img)) { 
  36.       $filePath = self::UPLOAD_PAHT . date('Ymd') . '/'
  37.       //判断文件上传路径,如果不存在,则创建 
  38.       if (!file_exists($filePath)) { 
  39.         @mkdir($filePath, 0777, true); 
  40.         @chmod($filePath, 0777); 
  41.       } 
  42.       //文件名,我们通过md5文件名加上扩展名 
  43.       $fileName = md5($head_img->baseName) . '.' . $head_img->extension; 
  44.       $file = $filePath . $fileName
  45.       //保存文件到我们的服务器上 
  46.       $head_img->saveAs($file); 
  47.       //返回服务器上的文件地址 
  48.       return $file
  49.     } else { 
  50.       return false; 
  51.     } 
  52.   } 

控制器代码如下:

  1. <?php 
  2.  
  3. namespace app\controllers; 
  4.  
  5. use YII; 
  6. use yii\web\Controller; 
  7.  
  8. class IndexController extends Controller 
  9.   public function actionIndex() 
  10.   { 
  11.     if (YII::$app->request->isPost) { 
  12.       $user = new \app\models\MyUser(); 
  13.  
  14.       //把POST过来的数据加载到user对象 
  15.       $data = YII::$app->request->post(); 
  16.       //注意第二个参数设为'',默认YII的ActiveForm创建的表单元素会加上下标 
  17.       $user->load($data''); 
  18.  
  19.       if ($user->validate()) { 
  20.         $user->pwd = YII::$app->security->generatePasswordHash($user->pwd); 
  21.         $user->head_img = $user->uploadHeadImg(); 
  22.  
  23.         //这里保存时设为false不验证,因为pwd加密了 
  24.         $user->save(false); 
  25.       } else { 
  26.         var_dump($user->errors); 
  27.       } 
  28.     } else { 
  29.       return $this->renderPartial('index'); 
  30.     } 
  31.   } 

这样我们就可以通过表单上传图像了。

YII2框架实现表单中上传单个文件的方法示例

YII2框架实现表单中上传单个文件的方法示例

Tags: YII2表单上传单个文件

分享到: