[英]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.