yii框架中文件上传
来源:互联网 发布:php json转数组为null 编辑:程序博客网 时间:2024/05/29 10:58
1:在数据库中建立一张表(upload,我的表是这样的:
2:使用Gii生成model层;(切记要model层和控制器层一定要有 use yii\web\UploadedFile;控制器一定要调用model哦)
- <?php
-
- namespace app\models;
- use yii\web\UploadedFile;
- use Yii;
-
- /**
- * This is the model class for table "upload".
- *
- * @property integer $u_id
- * @property string $u_name
- * @property string $u_pwd
- * @property string $u_img
- */
- class Upload extends \yii\db\ActiveRecord
- {
- /**
- * @inheritdoc
- */
- public static function tableName()
- {
- return 'upload';
- }
-
- /**
- * @inheritdoc
- */
- public function rules()
- {
- return [
- [['u_name', 'u_pwd', 'u_img'], 'string', 'max' => 255]
- ];
- }
-
- /**
- * @inheritdoc
- */
- public function attributeLabels()
- {
- return [
- 'u_id' => 'U ID',
- 'u_name' => 'U Name',
- 'u_pwd' => 'U Pwd',
- 'u_img' => 'U Img',
- ];
- }
- public function upload(){
- //$this->p_photo->saveAs('uploads/' . $this->p_photo->baseName . '.' . $this->p_photo->extension);
- //return true;
- return $this->u_img->saveAs('upload/' . $this->u_img->baseName . '.' . $this->u_img->extension);
-
-
- }
- }
3:建立好uploadController.php控制器,在没有post接值的时候,进入views/upload/entry.php,显示视图层,至于views/upload/entry-confirm.php视图,是你接入值后要执行的程序,这里我没有用到这个视图,当接到表单的值之后我又自己生成了个方法;- <?php
- namespace app\controllers;
-
- use Yii;
- use yii\filters\AccessControl;
- use yii\web\Controller;
- use yii\filters\VerbFilter;
- use app\models\LoginForm;
- use app\models\ContactForm;
- use yii\web\UploadedFile;
- use app\models\Upload;
- class UploadController extends Controller
- {
- public function actionIndex(){
- $model = new Upload();
-
- if ($model->load(Yii::$app->request->post()) && $model->validate()) {
- // 验证 $model 收到的数据
-
- // 做些有意义的事 ...
-
- return $this->render('entry-confirm', ['model' => $model]);
- } else {
- // 无论是初始化显示还是数据验证错误
- return $this->render('entry', ['model' => $model]);
- }
-
- }
- public function actionAdds()
- {
- $model = new Upload();
- $request = Yii::$app->request;
- //$post = $request->post();
- //print_r($data);
- $post=$request->post('Upload');
- $u_name = $post['u_name'];
- $u_pwd = $post['u_pwd'];
- //print_r($u_name);
- //在浏览器输出的值是 yii\web\UploadedFile Object ( [name] => 2.jpg [tempName] => C:\Windows\php3986.tmp
- // [type] => image/jpeg [size] => 216848 [error] => 0 )
- $arr = $model->u_img = UploadedFile::getInstance($model,'u_img');
- //print_r($arr);
- if ($model->upload()) {
- // 文件上传成功
- $u_img = $arr->name;
- //var_dump($u_img);
- $connection = \Yii::$app->db;
- $result=$connection->createCommand()->insert('upload', [
- 'u_name' => $u_name,
- 'u_pwd' => $u_pwd,
- 'u_img' =>$u_img,
- ])->execute();
- if($result)
- {
- echo "添加成功";
- }
- else
- {
- echo "添加失败";
- }
- }
-
- }
-
- }
- ?>
4:视图层显示- <?php
- use yii\helpers\Html;
- use yii\widgets\ActiveForm;
- ?>
- <?php $form = ActiveForm::begin([
- 'action' => ['upload/adds'],
- 'method'=>'post',
- 'options' => ['enctype' => 'multipart/form-data']
- ]); ?>
-
- <?= $form->field($model, 'u_name') ?>
-
- <?= $form->field($model, 'u_pwd') ?>
-
- <?=$form->field($model, 'u_img')->fileInput(['multiple'=>'multiple']);?>
- <div class="form-group">
- <?= Html::submitButton('Submit', ['class' => 'btn btn-primary']) ?>
- </div>
-
- <?php ActiveForm::end(); ?>
5:至于entry_form.PHP可以有,也可以不要它,因为上边我已经写了actionAdds()这个方法,这个就是当你接值成功列表显示的页面,可以这样写:
- <?php
- use yii\helpers\Html;
- ?>
- <p>You have entered the following information:</p>
-
- <ul>
- <li><label>Name</label>: <?= Html::encode($model->u_name) ?></li>
- <li><label>Email</label>: <?= Html::encode($model->u_pwd) ?></li>
- </ul>
-
- 好了,就这些了,会有更多方法给大家分享的哦!<img alt="吐舌头" src="http://static.blog.csdn.net/xheditor/xheditor_emot/default/tongue.gif" />