[英]Upload file in Laravel - wrong path
我是Laravel的初学者。 我有以下代码:
if ($request->hasfile('profilePhoto')) {
$this->validate($request, [
'profilePhoto' => 'required',
'profilePhoto.*' => 'mimetypes:image/jpg'
]);
$image = $request->file('profilePhoto');
$extension = strtolower($image->getClientOriginalExtension());
$path = 'upload/images/UserImage/';
$uniqueName = md5($image . time());
$image->move(public_path($path), $uniqueName . '.' . $extension);
}
此功能将文件上传到public/upload/images/UserImage/
。 我需要它将其存储在storage/app/upload/images/UserImage/
如何重写我的代码?
您必须使用storage_path
函数(必须存在“ storage / app / upload”文件夹):
$image->move(storage_path("app/upload"), $uniqueName . '.' . $extension);
if ($request->hasfile('profilePhoto')) { $this->validate($request, [ 'profilePhoto' => 'required', 'profilePhoto.*' => 'mimetypes:image/jpg' ]); $image = $request->file('profilePhoto'); $extension = strtolower($image->getClientOriginalExtension()); $path = storage_path('app/public/upload/images/UserImage'); $uniqueName = md5($image . time()); $image->move(public_path($path), $uniqueName . '.' . $extension); }
Laravel上传到storage/app
一种常见方法是通过本地磁盘驱动程序。
$file = $path . $uniqueName . '.' . $extension;
\Storage::disk('local')->put($file, $request->file('profilePhoto'));
Storage::disk('local')
指向storage/app/
。
因为$ path变量已经像$path = 'upload/images/UserImage/'
所以可以将数据存储到storage_path()
而不是将数据存储到public_path
。
$image->move(storage_path($path), $uniqueName . '.' . $extension);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.