且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Laravel获取文件名

更新时间:2021-12-31 19:37:22

  1. 要获取文件名,如文档中所述:

您可以使用file方法访问Illuminate \ Http \ Request实例中包含的上载文件. file方法返回的对象是Symfony \ Component \ HttpFoundation \ File \ UploadedFile类的实例,该类扩展了PHP SplFileInfo类并提供了多种与文件交互的方法

You may access uploaded files that are included with the Illuminate\Http\Request instance using the file method. The object returned by the file method is an instance of the Symfony\Component\HttpFoundation\File\UploadedFile class, which extends the PHP SplFileInfo class and provides a variety of methods for interacting with the file

UploadedFile实例上还有许多其他方法可用.有关详细信息,请查看该类的API文档关于这些方法.

There are a variety of other methods available on UploadedFile instances. Check out the API documentation for the class for more information regarding these methods.

因此您可以使用以下方法:getClientOriginalName()

So you can use this method: getClientOriginalName()

http://api.symfony.com/3.0/Symfony/Component/HttpFoundation/File/UploadedFile.html#method_getClientOriginalName

$request->file('input_pdf')->getClientOriginalName();

将返回文件名.

您可以在调用文件上的任何方法之前执行以下操作以检查文件是否存在:

You can do this to check if the file exists before calling any methods on it:

if ($request->hasFile('input_pdf')) {
    return $request->file('input_pdf')->getClientOriginalName();
} else {
    return 'no file!'
}

  1. 要解决dd($request->file('input_pdf'))返回null的问题,请检查您是否使用了正确的文件名.您可以尝试dd($request),然后查看其中是否有文件.查看Request对象的转储时,可以检查文件名.
  1. To solve the issue of dd($request->file('input_pdf')) returning null check you are using the correct name for the file. You can try dd($request) and you will see if there are any files in it. You can check the file name when reviewing the dump of the Request object.