自用找方法 有不完善的地方
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UploadsController extends Controller
{
public function upload(Request $request){
$data =['123','456'];
$arr =['errno'=>'0',$data];
Log::error('123456');
$error='asdfadsfasdf';
Log::emergency($error);
Log::alert($error);
Log::critical($error);
Log::error($error);
Log::warning($error);
Log::notice($error);
Log::info($error);
Log::debug($error);
return $data;
// dd($request->all());
$res = '没有上传';
if ($request->hasFile('editor-file')) {
//
$file = $request->file('editor-file');
$data = $request->all();
$rules = [
'editor-file' => 'max:5120',
];
$messages = [
'editor-file.max' => '文件过大,文件大小不得超出5MB',
];
$validator = Validator($data, $rules, $messages);
// $validator = $this->validate($data, $rules, $messages);
$res = 'error|失败原因为:非法传参';
if ($validator->passes()) {
$realPath = $file->getRealPath();
$destPath = '/uploads';
$savePath = $destPath.''.date('Ymd', time());
is_dir($savePath) || mkdir($savePath); //如果不存在则创建目录
$name = $file->getClientOriginalName();
$ext = $file->getClientOriginalExtension();
$check_ext = in_array($ext, ['gif', 'jpg', 'jpeg', 'png'], true);
if ($check_ext) {
$uniqid = uniqid().'_'.date('s');
$oFile = $uniqid.'o.'.$ext;
$fullfilename = '/'.$savePath.'/'.$oFile; //原始完整路径
if ($file->isValid()) {
$uploadSuccess = $file->move($savePath, $oFile); //移动文件
$oFilePath = $savePath.'/'.$oFile;
$res = $fullfilename;
$res['errno'] = 0;
$res['data'] = $fullfilename;
} else {
$res = 'error|失败原因为:文件校验失败';
}
} else {
$res = 'error|失败原因为:文件类型不允许,请上传常规的图片(gif、jpg、jpeg与png)文件';
}
} else {
$res = 'error|'.$validator->messages()->first();
}
}
return $res;
}
public function upload2(Request $request)
{
dd(2);
$tempFile =[];
$pics = $request->file('editor-file');
if($request->hasFile('editor-file')){
$tempFile['errno'] = 0;
// foreach($pics as $file) {
// $destinationPath = '/uploads';
// $extension = $file->getClientOriginalExtension();
// $fileName = time().rand(10000,99999).'.'.$extension;
// $path = $file->move($destinationPath, $fileName);
$tempFile['data'][] = $path->getRealPath();
// $tempFile['data'][] = 'http://e.hiphotos.baidu.com/image/pic/item/18d8bc3eb13533faf60894e4a1d3fd1f40345bb9.jpg';
// // array_push($params['multipart'] , ['name' => 'carousel[]' , 'contents' => fopen($path->getRealPath(), 'r')]);
// }
$destinationPath = './uploads';
$extension = $pics->getClientOriginalExtension();
$fileName = time().rand(11111,99999).'.'.$extension;
$path = $pics->move($destinationPath, $fileName);
// array_push($params['multipart'] , ['name' => 'cover' , 'contents' => fopen($path->getRealPath(), 'r')]);
$tempFile['data'][] = $path->getRealPath();
}else{
$tempFile['errno'] = '未找到图片';
}
// // 返回图片的URL地址
// $data['data'][0] = 'http://e.hiphotos.baidu.com/image/pic/item/18d8bc3eb13533faf60894e4a1d3fd1f40345bb9.jpg';
// $data['data'][1] = 'http://e.hiphotos.baidu.com/image/pic/item/f3d3572c11dfa9ec665057dd6bd0f703908fc1fa.jpg';
return json_encode($tempFile);
}
public function upload3(Request $request)
{
$tempFile = array();
$tempFile['errno']=0;
$tempFile['data'][0] = 'http://e.hiphotos.baidu.com/image/pic/item/18d8bc3eb13533faf60894e4a1d3fd1f40345bb9.jpg';
$tempFile['data'][1] = 'http://e.hiphotos.baidu.com/image/pic/item/f3d3572c11dfa9ec665057dd6bd0f703908fc1fa.jpg';
$url['url'] = 'http://e.hiphotos.baidu.com/image/pic/item/18d8bc3eb13533faf60894e4a1d3fd1f40345bb9.jpg';
// return json_encode($tempFile);
// echo 123;
return json_encode($url);
}
public function upload4(Request $request){
dd(4);
if ($request->hasFile('editor-file')) {
//
$file = $request->file('editor-file');
$data = $request->all();
$rules = [
'editor-file' => 'max:5120',
];
$messages = [
'editor-file.max' => '文件过大,文件大小不得超出5MB',
];
$validator = Validator($data, $rules, $messages);
// $validator = $this->validate($data, $rules, $messages);
$res = 'error|失败原因为:非法传参';
if ($validator->passes()) {
$realPath = $file->getRealPath();
$destPath = 'uploads/content/';
$savePath = $destPath.''.date('Ymd', time());
is_dir($savePath) || mkdir($savePath); //如果不存在则创建目录
$name = $file->getClientOriginalName();
$ext = $file->getClientOriginalExtension();
$check_ext = in_array($ext, ['gif', 'jpg', 'jpeg', 'png'], true);
if ($check_ext) {
$uniqid = uniqid().'_'.date('s');
$oFile = $uniqid.'o.'.$ext;
$fullfilename = '/'.$savePath.'/'.$oFile; //原始完整路径
if ($file->isValid()) {
$uploadSuccess = $file->move($savePath, $oFile); //移动文件
$oFilePath = $savePath.'/'.$oFile;
$res = $fullfilename;
} else {
$res = 'error|失败原因为:文件校验失败';
}
} else {
$res = 'error|失败原因为:文件类型不允许,请上传常规的图片(gif、jpg、jpeg与png)文件';
}
} else {
$res = 'error|'.$validator->messages()->first();
}
}
return $res;
}
}