php Laravel 获取文件名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37161505/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:06:07  来源:igfitidea点击:

Laravel get name of file

phpfilelaravellaravel-5.1

提问by John Does Legacy

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

我基本上只想得到一个文件的名称,我得到这样的:

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

if I dd($inputPdf)it prints me null.

如果我dd($inputPdf)打印我null

回答by haakym

  1. To get the file name, as mentioned in the docs:
  1. 要获取文件名,如文档中所述:

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

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

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

UploadedFile 实例上还有多种其他方法可用。有关这些方法的更多信息,请查看该类API 文档

So you can use this method: getClientOriginalName()

所以你可以使用这个方法: getClientOriginalName()

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

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

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

Would return the file name.

将返回文件名。

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. To solve the issue of dd($request->file('input_pdf'))returning nullcheck 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 Requestobject.
  1. 为了解决dd($request->file('input_pdf'))返回null支票的问题,您使用了正确的文件名称。你可以试试dd($request),看看里面有没有文件。您可以在查看Request对象的转储时检查文件名。

回答by ExohJosh

$request->file->getClientOriginalName();

Check thisfor more methods.

检查这个以获取更多方法。

回答by Parvez Rahaman

I think you forget to add

我想你忘了添加

enctype="multipart/form-data"

In your form. Or if you used laravel HTML service provider use

以你的形式。或者,如果您使用 laravel HTML 服务提供程序,请使用

{!! Form::open(array('files' => true)) !!}