-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathAttachController.php
executable file
·51 lines (38 loc) · 1.31 KB
/
AttachController.php
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
<?php
/**
* Created by PhpStorm.
* User: sdf_sky
* Date: 2017/2/4
* Time: 下午3:12
*/
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Validator;
class AttachController extends Controller
{
public function upload(Request $request){
$validator = Validator::make($request->all(),[
'file' => 'required|max:'.config('tipask.upload.attach_size'),
]);
if($validator->fails()){
return $this->ajaxError(10000,$validator->errors()->first('file'));
}
$file = $request->file('file');
$extension = $file->getClientOriginalExtension();
$filePath = 'attachments/'.gmdate("Y")."/".gmdate("m")."/".uniqid(str_random(8)).'.'.$extension;
Storage::disk('local')->put($filePath,File::get($file));
return $this->ajaxSuccess([
'url'=>route("website.attach.download",['name'=>str_replace("/","-",$filePath)]),
'name'=>$file->getClientOriginalName()
]);
}
public function download($name){
$attachFile = storage_path('app/'.str_replace("-","/",$name));
if(!is_file($attachFile)){
abort(404);
}
return response()->download($attachFile);
}
}