Pārlūkot izejas kodu

文件上传校验

linwu 1 gadu atpakaļ
vecāks
revīzija
c4148166cf

+ 9 - 2
app/admin/controller/Attachment.php

@@ -44,6 +44,7 @@ class Attachment
 	// 图片上传
 	public function tplFieldImage()
 	{
+        $this->validateExt(['jpg,png,jpeg']);
 		$file = request()->file("file");
 		$savename = \think\facade\Filesystem::disk('public')->putFile('images',$file);
 		if($file){
@@ -187,6 +188,12 @@ class Attachment
 			'msg'	=> "删除失败,请稍后重试"
 		)));
 	}
-	
-	
+
+    public function validateExt($ext_arr)
+    {
+        $ext = pathinfo($_FILES['file']['name'])['extension'];
+        if (!in_array($ext,$ext_arr)) {
+            page_result(1, "文件格式不正确");
+        }
+    }
 }

+ 9 - 2
app/agent/controller/Attachment.php

@@ -45,6 +45,7 @@ class Attachment
 	// 图片上传
 	public function tplFieldImage()
 	{
+        $this->validateExt(['jpg,png,jpeg']);
 		$file = request()->file("file");
 		$savename = \think\facade\Filesystem::disk('public')->putFile('images',$file);
 		if($file){
@@ -70,7 +71,13 @@ class Attachment
 			)));
 		}
 	}
-	
-	
+
+    public function validateExt($ext_arr)
+    {
+        $ext = pathinfo($_FILES['file']['name'])['extension'];
+        if (!in_array($ext,$ext_arr)) {
+            page_result(1, "文件格式不正确");
+        }
+    }
 	
 }

+ 10 - 1
app/mainapp/controller/Attachment.php

@@ -13,6 +13,7 @@ class Attachment
 	// 单个图片上传
 	public function tplFieldImage()
 	{
+        $this->validateExt(['jpg,png,jpeg']);
 		$file = request()->file("file");
 		$savename = \think\facade\Filesystem::disk('public')->putFile('images',$file);
 		if($file){
@@ -39,6 +40,7 @@ class Attachment
 	// 视频文件上传
 	public function tplFieldVideo()
 	{
+        $this->validateExt(['mp4,avi,wmv,mpg,mpeg']);
 		$file = request()->file("file");
 		$savename = \think\facade\Filesystem::disk('public')->putFile('videos',$file);
 		if($file){
@@ -58,5 +60,12 @@ class Attachment
 			page_result(1, "上传失败,请稍后重试");
 		}
 	}
-	
+
+	public function validateExt($ext_arr)
+    {
+        $ext = pathinfo($_FILES['file']['name'])['extension'];
+        if (!in_array($ext,$ext_arr)) {
+            page_result(1, "文件格式不正确");
+        }
+    }
 }

+ 9 - 2
app/worker/controller/Attachment.php

@@ -45,6 +45,7 @@ class Attachment
 	// 图片上传
 	public function tplFieldImage()
 	{
+        $this->validateExt(['jpg,png,jpeg']);
 		$file = request()->file("file");
 		$savename = \think\facade\Filesystem::disk('public')->putFile('images',$file);
 		if($file){
@@ -70,7 +71,13 @@ class Attachment
 			)));
 		}
 	}
-	
-	
+
+    public function validateExt($ext_arr)
+    {
+        $ext = pathinfo($_FILES['file']['name'])['extension'];
+        if (!in_array($ext,$ext_arr)) {
+            page_result(1, "文件格式不正确");
+        }
+    }
 	
 }