Laravel 本地上传图片 怎么获得完整的图片路径插入数据库
$realPath = $file->getRealPath(); // 文件的绝对路径
$filename = '/chat/'.date('Ymd').'/'.date('Ymdhis').'-'.mt_rand(100,999).'.'.$ext; // 定义文件名
$res = Storage::disk('public')->put($filename, file_get_contents($realPath)); // 本地存储
if( $res){
// 怎么获取完整路径插入数据库?
}
//跟帖
// 用了这种写法 不知道规不规范
$url = config('filesystems.disks.public.url').$filename;
//跟帖 谢谢大佬指点
$disk = Storage::disk('public');
$res = $disk->put($filename, file_get_contents($realPath)); // 本地存储
if( !$res ){
return Y::error('上传失败local');
}
$url = $disk->url($filename);
代码加上空格吧,使用url方法,传入path,将会为你自动拼接驱动配置的url参数。