您当前的位置: 首页 >  postman

暂无认证

  • 4浏览

    0关注

    92582博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

yii2多文件上传接口开发(特色,拥有时间目录,数据能保存到数据库中,postman测试,)

发布时间:2022-05-05 05:46:03 ,浏览量:4

如果你想到调通接口,请看我的文章。看完我的调通接口文章后,请在api/web/下新建一个uploads目录

在这里插入图片描述 postman: 在这里插入图片描述 在这里插入图片描述 gii:生成model:一次能上传10个文件

public $file; /**
     * @inheritdoc
     */ public static function tableName() { return 'upmore'; } /**
     * @inheritdoc
     */ public function rules() { return [ [['file'], 'file', 'maxFiles' => 10,'extensions'=>'jpg,png,gif,txt,doc'], ]; } /**
     * @inheritdoc
     */ public function attributeLabels() { return [ 'id' => 'ID', 'name' => 'Name', 'path' => 'Path', 'time' => 'Time', ]; } } : 
 public $enableCsrfValidation = false; public function actionCreate(){ $model=new Upmore(); if (Yii::$app->request->isPost) { $file = UploadedFile::getInstances($model, 'file'); $path='uploads/'.date("YmdH",time()).'/'; if ($file && $model->validate()) { if(!file_exists($path)){ mkdir($path,0775,true); } foreach ($file as $fl) { $fl->saveAs($path . $fl->baseName. '.' . $fl->extension); Yii::$app->db->createCommand()->insert('upmore', [ 'path'=>"/".$path.$fl->baseName. '.' . $fl->extension, 'name' =>$fl->baseName. '.' . $fl->extension, 'time'=>date('Y-m-d H:i:s') ])->execute(); } } } } } 

在这里插入图片描述

关注
打赏
1653961664
查看更多评论
立即登录/注册

微信扫码登录

0.6393s