Laravel获取文件名 [英] Laravel get name of file

查看:1316
本文介绍了Laravel获取文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基本上只是想获得一个像这样的文件名:

I basically just want to get the name of a file which I get like this:

$inputPdf = $request->file('input_pdf');

如果我dd($inputPdf)它会打印我null.

推荐答案

  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.

这篇关于Laravel获取文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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