yii框架中文件上传

来源:互联网 发布:php json转数组为null 编辑:程序博客网 时间:2024/05/29 10:58

 1:在数据库中建立一张表(upload,我的表是这样的:


2:使用Gii生成model层;(切记要model层和控制器层一定要有 use yii\web\UploadedFile;控制器一定要调用model哦)

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?php  
  2.   
  3. namespace app\models;  
  4. use yii\web\UploadedFile;  
  5. use Yii;  
  6.   
  7. /**  
  8.  * This is the model class for table "upload".  
  9.  *  
  10.  * @property integer $u_id  
  11.  * @property string $u_name  
  12.  * @property string $u_pwd  
  13.  * @property string $u_img  
  14.  */  
  15. class Upload extends \yii\db\ActiveRecord  
  16. {  
  17.     /**  
  18.      * @inheritdoc  
  19.      */  
  20.     public static function tableName()  
  21.     {  
  22.         return 'upload';  
  23.     }  
  24.   
  25.     /**  
  26.      * @inheritdoc  
  27.      */  
  28.     public function rules()  
  29.     {  
  30.         return [  
  31.             [['u_name', 'u_pwd', 'u_img'], 'string', 'max' => 255]  
  32.         ];  
  33.     }  
  34.   
  35.     /**  
  36.      * @inheritdoc  
  37.      */  
  38.     public function attributeLabels()  
  39.     {  
  40.         return [  
  41.             'u_id' => 'U ID',  
  42.             'u_name' => 'U Name',  
  43.             'u_pwd' => 'U Pwd',  
  44.             'u_img' => 'U Img',  
  45.         ];  
  46.     }  
  47.      public function upload(){  
  48.         //$this->p_photo->saveAs('uploads/' . $this->p_photo->baseName . '.' . $this->p_photo->extension);  
  49.         //return true;  
  50.         return $this->u_img->saveAs('upload/' . $this->u_img->baseName . '.' . $this->u_img->extension);  
  51.   
  52.       
  53. }  
  54. }  
3:建立好uploadController.php控制器,在没有post接值的时候,进入views/upload/entry.php,显示视图层,至于views/upload/entry-confirm.php视图,是你接入值后要执行的程序,这里我没有用到这个视图,当接到表单的值之后我又自己生成了个方法;

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?php  
  2. namespace app\controllers;  
  3.   
  4. use Yii;  
  5. use yii\filters\AccessControl;  
  6. use yii\web\Controller;  
  7. use yii\filters\VerbFilter;  
  8. use app\models\LoginForm;  
  9. use app\models\ContactForm;  
  10. use yii\web\UploadedFile;  
  11. use app\models\Upload;  
  12. class UploadController extends Controller  
  13. {  
  14.     public function actionIndex(){  
  15.         $model = new Upload();  
  16.   
  17.         if ($model->load(Yii::$app->request->post()) && $model->validate()) {  
  18.             // 验证 $model 收到的数据  
  19.   
  20.             // 做些有意义的事 ...  
  21.   
  22.             return $this->render('entry-confirm', ['model' => $model]);  
  23.         } else {  
  24.             // 无论是初始化显示还是数据验证错误  
  25.             return $this->render('entry', ['model' => $model]);  
  26.         }  
  27.   
  28.     }  
  29.     public function actionAdds()  
  30.     {  
  31.         $model = new Upload();  
  32.         $request = Yii::$app->request;  
  33.         //$post = $request->post();  
  34.         //print_r($data);  
  35.          $post=$request->post('Upload');  
  36.         $u_name = $post['u_name'];  
  37.         $u_pwd = $post['u_pwd'];  
  38.         //print_r($u_name);  
  39.         //在浏览器输出的值是 yii\web\UploadedFile Object ( [name] => 2.jpg [tempName] => C:\Windows\php3986.tmp  
  40.         // [type] => image/jpeg [size] => 216848 [error] => 0 )   
  41.          $arr =  $model->u_img = UploadedFile::getInstance($model,'u_img');  
  42.          //print_r($arr);  
  43.           if ($model->upload()) {  
  44.                 // 文件上传成功  
  45.                 $u_img = $arr->name;  
  46.                 //var_dump($u_img);  
  47.                  $connection = \Yii::$app->db;  
  48.             $result=$connection->createCommand()->insert('upload', [  
  49.                 'u_name' => $u_name,  
  50.                 'u_pwd' => $u_pwd,  
  51.                  'u_img' =>$u_img,  
  52.             ])->execute();  
  53.             if($result)  
  54.             {  
  55.               echo "添加成功";    
  56.             }  
  57.             else  
  58.             {  
  59.                 echo "添加失败";  
  60.             }  
  61.             }  
  62.   
  63.     }  
  64.       
  65. }  
  66. ?>  
4:视图层显示

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?php  
  2. use yii\helpers\Html;  
  3. use yii\widgets\ActiveForm;  
  4. ?>  
  5. <?php $form = ActiveForm::begin([  
  6.     'action' => ['upload/adds'],  
  7.     'method'=>'post',  
  8.     'options' => ['enctype' => 'multipart/form-data']  
  9. ]); ?>  
  10.   
  11.     <?= $form->field($model, 'u_name') ?>  
  12.   
  13.     <?= $form->field($model, 'u_pwd') ?>  
  14.       
  15.     <?=$form->field($model, 'u_img')->fileInput(['multiple'=>'multiple']);?>  
  16.     <div class="form-group">  
  17.         <?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?>  
  18.     </div>  
  19.   
  20. <?php ActiveForm::end(); ?>  

5:至于entry_form.PHP可以有,也可以不要它,因为上边我已经写了actionAdds()这个方法,这个就是当你接值成功列表显示的页面,可以这样写:

[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <?php  
  2. use yii\helpers\Html;  
  3. ?>  
  4. <p>You have entered the following information:</p>  
  5.   
  6. <ul>  
  7.     <li><label>Name</label><?= Html::encode($model->u_name) ?></li>  
  8.     <li><label>Email</label><?= Html::encode($model->u_pwd) ?></li>  
  9. </ul>  
  10.   
  11. 好了,就这些了,会有更多方法给大家分享的哦!<img alt="吐舌头" src="http://static.blog.csdn.net/xheditor/xheditor_emot/default/tongue.gif" />  
原创粉丝点击