/**
* 上传文件
* @param Request $request
* @return array
*/
public function upload(Request $request){
$folder = 'Uploads/'.date('Ymd');
if(!Storage::disk('public')->exists($folder)){
Storage::makeDirectory($folder);
}
$files = $request->allFiles();//获取所有文档
$fileid=[];
foreach ($files as $file){
if($file->isValid()) {
$newFileName = md5(microtime()) . '.' . $file->getClientOriginalExtension();
Storage::disk('public')->put($folder . '/' . $newFileName, file_get_contents($file));
//文件类型
$type = $file->getClientMimeType();
$size = $file->getSize();
$data = File::create(['filename' => $newFileName, 'filepath' => $folder,'md5' => '', 'mime' => $type, 'size' => $size]);
array_push($fileid,$data->id);
}
}
//return response
return ['fileid'=>implode(',',$fileid)];
}
/**
* 删除文件
* @param Request $request
* @return array
*/
public function destroy(Request $request)
{
$filename = explode(',',$request->input('filename'));
$files = File::whereIn('filename',$filename)->get();
$id = [];
foreach ($files as $file){
Storage::disk('public')->delete($file->filepath.'/'.$file->filename);
$id[] = $file->id;
}
$result = File::destroy($id);
if($result){
return redirect()->back()->with('success','删除成功');
}else{
return redirect()->back()->with('error','删除失败');
}
}