1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
|
protected function has_extension(string $extension) { return in_array($extension, ['jpg', 'jpeg', 'png', 'gif', 'bmp']); }
public function index(Request $request) { $name = $request->input('name', 'file');
if($request->hasFile($name)) {
$file = $request->file($name); $extension = $file->extension();
if(!$this->has_extension($extension)) { return '格式错误'; }
$filename = $file->store(); } else if(preg_match('/^(data:\s*image\/(\w+);base64,)/', $request->input($name), $matches)) {
$extension = $matches[2];
if(!$this->has_extension($extension)) { return '格式错误'; }
$file = $request->input($name); $file = preg_replace('/^(data:\s*image\/(\w+);base64,)/', '', $file); $file = str_replace(' ', '+', $file);
$filename = str_random(40) . '.' . $extension; Storage::put($filename, base64_decode($file)); } else { return '格式错误'; } return '上传成功[' . $filename . ']'; }
|