<?php namespace Api\Controller; header("Access-Control-Allow-Origin: *"); header("Access-Control-Allow-Methods' 'GET, POST'"); use Think\Controller; class ImgBankUploadController extends Controller { public $uploadRoute; public $uploadDocx; public function __construct() { parent::__construct(); //图片文件路径 $this->uploadRoute = "/Public/Uploads/Industry/" . date("Y") . '/' . date("m") . '/' . date("d") . '/'; //文档上传路径 $this->uploadDocx = "/Public/Uploads/Authority/" . date("Y") . '/' . date("m") . '/' . date("d") . '/'; } public function index() { foreach ($_FILES as $key => $v) { //文件类型 $type = end(explode('.', $v['name'])); } $time = time(); $getRandomString = $this->getRandomString(8); $upload = new \Think\Upload(); // 实例化上传类 $upload->maxSize = 471859; // 设置附件上传大小 $upload->saveName = array('uniqid', $time . $getRandomString); // $upload->exts = array('jpg', 'gif', 'png', 'jpeg','docx');// 设置附件上传类型 $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型 $upload->saveExt = 'jpg'; $this->getFile($type); //创建文件夹 if ($type == 'docx') { $upload->rootPath = $_SERVER['DOCUMENT_ROOT'] . $this->uploadDocx; // 设置附件上传根目录 } else { $upload->rootPath = $_SERVER['DOCUMENT_ROOT'] . $this->uploadRoute; // 设置附件上传根目录 } $upload->savePath = ''; // 设置附件上传(子)目录 // 上传文件 $info = $upload->upload(); if (!$info) {// 上传错误提示错误信息 echo json_encode(['code' => 2, 'msg' => $upload->getError(), 'url' => $url2]); exit; } else {// 上传成功 foreach ($info as $file) { $url = $file['savepath'] . $file['savename']
PHP异步上传处理
最新推荐文章于 2023-03-06 13:57:52 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)