如何从存储方法中获取上载的图像名称 [英] How to get the uploaded image name from the store method

查看:71
本文介绍了如何从存储方法中获取上载的图像名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我通过以下方式将图像存储在Laravel中时:

When I store an image in Laravel by doing:

$path = $request->file('myImage')->store('public/src/');

它返回完整路径,但是我如何只获得给定的文件名?

It returns the full path, but how do I get only the filename it was given?

这是返回路径的示例:

public/src/ltX4COwEmvxVqX4Lol81qfJZuPTrQO6S2jsicuyp.png

推荐答案

在Laravel中,store()方法动态生成名称..所以您不能从store()方法中获取它.

In Laravel, the store() method generates the name dynamically .. so you can't get it from the store() method.

但是您可以使用storeAs()方法.基本上store()方法正在调用storeAs()方法.所以:

But you can use storeAs() method. Basically the store() method is calling the storeAs() method. So:

$path = $request->file('myImage')->store('public/src');

Laravel在做什么,正在调用->storeAs('public/src', $request->file('myImage')->hashName()); ..您看到了hashName()方法吗?就是这个名字的产生.

What Laravel is doing is calling ->storeAs('public/src', $request->file('myImage')->hashName()); .. you see the hashName() method? that is what generates the name.

因此,您可以先致电hashName()并在存储发生之前知道您的名字..这是一个示例:

So you can call hashName() first and know your name before the storing happens .. here is an example:

$uploadFile = $request->file('myImage');
$file_name = $uploadFile->hashName();
$path = $uploadFile->storeAs('public/src', $file_name);

现在您有$file_name$path.

请参阅:

  • https://laravel.com/docs/6.x/filesystem#file-uploads .. Specifying A File Name
  • https://github.com/laravel/framework/blob/6.x/src/Illuminate/Http/UploadedFile.php#L33
  • https://github.com/laravel/framework/blob/6.x/src/Illuminate/Http/FileHelpers.php#L42

这篇关于如何从存储方法中获取上载的图像名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆