简体   繁体   English

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

[英]How can i get the correct file extension in laravel 5.4

When I upload a csv file in laravel 5.4, I get 'txt' as the extension.当我在 laravel 5.4 中上传一个 csv 文件时,我得到了“txt”作为扩展名。 Is there something I'm missing?有什么我想念的吗?

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);
 }

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

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM