如果你想到调通接口,请看我的文章。看完我的调通接口文章后,请在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(); } } } } }