如何在 laravel 5.4 中获得正确的文件扩展名

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

How can i get the correct file extension in laravel 5.4

phplaravelfile-uploadlaravel-5.4

提问by mojoblanco

When I upload a csv file in laravel 5.4, I get 'txt' as the extension. Is there something I'm missing?

当我在 laravel 5.4 中上传一个 csv 文件时,我得到了“txt”作为扩展名。有什么我想念的吗?

View

看法

{!! Form::open(['url' => 'transaction/save', 'files' => true]) !!}

{!! Form::file('batch'); !!}

{!! Form::submit('Upload') !!}

{!! Form::close() !!}

Controller

控制器

public function saveBatch(Request $request)
 {
        $file = $request->batch;
        $path = $request->batch->extension();

        dd($path);
 }

回答by apokryfos

You need to move the file first, if you don't then the file is actually a temp file with no extension. You could also use:

您需要先移动文件,否则该文件实际上是一个没有扩展名的临时文件。您还可以使用:

 $request->batch->getClientOriginalExtension();

This would return the original filename's extension. More methods at: http://api.symfony.com/3.0/Symfony/Component/HttpFoundation/File/UploadedFile.html

这将返回原始文件名的扩展名。更多方法请访问:http: //api.symfony.com/3.0/Symfony/Component/HttpFoundation/File/UploadedFile.html

回答by Volkan Metin

The best way to get the extension of uploaded file: (If file input name is file)

获取上传文件扩展名的最佳方法:(如果文件输入名称是文件)

$extension = $request->file->extension();