123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- <?php
- namespace app\common\api;
- /**
- * Description of UploadApi
- *
- * @author sgq
- */
- class UploadApi {
- protected $fileSize = 5 * 1024 * 1024;
- protected $fileExt = "jpg,png,gif,pdf,xls,xlsx";
- protected $fileMime = "image/jpg,image/png,image/gif,application/pdf,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
- public function __construct($config = []) {
- $this->fileSize = isset($config["maxSize"]) ? $config["maxSize"] : $this->fileSize;
- $this->fileExt = isset($config["fileExt"]) ? $config["fileExt"] : $this->fileExt;
- $this->fileMime = isset($config["fileMime"]) ? $config["fileMime"] : $this->fileMime;
- }
- /**
- * 批量上传文件
- * @param type $files
- * @return type
- */
- public function uploadList($files, $path = "uploadfiles") {
- try {
- validate(['image' => ["sileSize" => $this->fileSize, "fileExt" => $this->fileExt, "fileMime" => $this->fileMime]])
- ->check($files);
- $savename = [];
- foreach ($files as $file) {
- $savename[] = \think\facade\Filesystem::disk("public")->putFile($path, $file);
- }
- return $savename;
- } catch (\think\exception\ValidateException $e) {
- echo $e->getMessage();
- }
- }
- /**
- * 单个文件上传
- * @param type $file
- * @return type
- */
- public function uploadOne($file, $path = "uploadfiles") {
- try {
- validate(['image' => ["sileSize" => $this->fileSize, "fileExt" => $this->fileExt, "fileMime" => $this->fileMime]])
- ->check([$file]);
- $savename = \think\facade\Filesystem::disk("public")->putFile($path, $file);
- return $savename;
- } catch (\think\exception\ValidateException $e) {
- echo $e->getMessage();
- }
- }
- }
|