wugg 6 months ago
parent
commit
b146b145e5
1 changed files with 26 additions and 2 deletions
  1. 26 2
      app/common.php

+ 26 - 2
app/common.php

@@ -10,7 +10,7 @@
 // \二つ
 // 应用公共文件
 //失败返回值
-if(!function_exists("error")){
+use think\facade\Filesystem;if(!function_exists("error")){
 	/**
 	* @param $message
 	* @param int $code
@@ -89,4 +89,28 @@ if(!function_exists('__')){
 	function __($msg){
 		return $msg;
 	}
-};
+};
+if(!function_exists('UploadImg')){
+    function UploadImg($files){
+         $savename = [];
+    $files = !is_array($files) ? [$files] : $files;
+    try {
+        //验证
+        validate(['imgFile' => ['fileSize' => 10240000, 'fileExt' => 'jpg,jpeg,png,bmp,gif', 'fileMime' => 'image/jpeg,image/png,image/gif']])->check(['imgFile' => $files]);
+
+        foreach ($files as $file) {
+            $url = Filesystem::disk('public')->putFile('topic/' . date('Ymd'), $file, function () use ($file) {
+                return str_replace('.' . $file->getOriginalExtension(), '', $file->getOriginalName() . '_' . date('YmdHis'));
+            });
+            $name = str_replace('.' . $file->getOriginalExtension(), '', $file->getOriginalName());
+            $temp = ['url' => $url, 'name' => $name];
+            $savename[] = $temp;
+        }
+
+        return $savename;
+    } catch (\think\exception\ValidateException $e) {
+            throw new \think\Exception($e->getMessage());
+    }
+    }
+
+}