laravel 上传文件
1. 在config/filesystems.php的数组中添加 // 新建一个本地端uploads空间(目录) 用于存储上传的文件'uploads' => [
'driver' => 'local',
// 文件将上传到storage/app/uploads目录
'root' => storage_path('app/uploads'),
// 文件将上传到public/uploads目录 如果需要浏览器直接访问 请设置成这个
//'root' => public_path('uploads'),
],
2. 跑到控制器里 开始造
public function create(Request $request)
{
$file = $request->file('file');
//判断文件是否上传成功
if ($file->isValid()){
//原文件名
$originalName = $file->getClientOriginalName();
//扩展名
$ext = $file->getClientOriginalExtension();
//MimeType
$type = $file->getClientMimeType();
//临时绝对路径
$realPath = $file->getRealPath();
// 拼个文件名出来瞅瞅
$filename = uniqid().'.'.$ext;
$bool = Storage::disk('uploads')->put($filename,file_get_contents($realPath));
//判断是否上传成功
if($bool){
downjson(0,'上传成功');
}else{
downjson(1,'上传失败');
}
}else{
downjson(1,'请注意文件类型');
}
}
参考文献: https://www.cnblogs.com/fps2tao/p/8007312.html
页:
[1]