Laravel 从上传获取保存文件的路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52339065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel get path of saved file from upload
提问by mike
I have a laravel upload for file (along with other data that is passed to the database) Everything works. But I just can't figure out how to save the path of the file that is saved.
我有一个 laravel 上传文件(以及传递给数据库的其他数据)一切正常。但我就是不知道如何保存所保存文件的路径。
Here is my controller function:
这是我的控制器功能:
public function store(Request $request)
{
request()->validate([
'name' => 'required',
'logo' => 'nullable',
'original_filename' => 'nullable',
]);
//This is where the file uploads?
if ($request->hasFile('logo')) {
$request->file('logo')->store('carrier_logo');
$request->merge([
'logo' => '',//TODO: get file location
'original_filename' => $request->file('logo')->getClientOriginalName(),
]);
}
Carrier::create($request->all());
return redirect()->route('carriers.index')->with('toast', 'Carrier created successfully.');
}
The thing I want to achieve:
I want logo
to fill with something like carrier_logo/ZbCG0lnDkUiN690KEFpLrNcn2exPTB8mUdFDwAKN.png
我想达到的目标:我想logo
用类似的东西来填充carrier_logo/ZbCG0lnDkUiN690KEFpLrNcn2exPTB8mUdFDwAKN.png
The thing that happened every time I tried to fix it was that it placed the temp path in the database. Which ended up being something in the PHP install directory.
每次我尝试修复它时都会发生的事情是它将临时路径放在数据库中。最终成为 PHP 安装目录中的内容。
回答by mcklayin
回答by Ali Hassan
just assign the value like this.
只需像这样分配值。
$location=base_path('img/'.$filename);
and save it in db.
并将其保存在数据库中。
回答by Chukwuemeka Inya
You could do this:
你可以这样做:
For FileName
对于文件名
$fileName = $request->file('test')->getClientOriginalName();
OR
或者
$fileName = $request->user()->id.'.'.$request->file('logo')->getClientOriginalExtension();
$imageDirectory = 'logo_images';
$path = $request->file('logo')->storeAs($imageDirectory, $fileName);
dd($path);