dongshen2903 2018-09-14 20:50
浏览 1218

Laravel从上传中获取保存文件的路径

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.

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

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.

  • 写回答

3条回答 默认 最新

  • duanqiang5722 2018-09-14 20:59
    关注

    Just assign result to variable:

    $path = $request->file('logo')->store('carrier_logo');
    

    According to docs

    Then you can do with $path variable whatever you want.

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?