php 从 Laravel 中上传的文件中获取图像扩展名

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

Get an image extension from an uploaded file in Laravel

phplaravelfilesystems

提问by Italo Borges

I have been trying to get the extension from an uploaded file, searching on google, I got no results.

我一直试图从上传的文件中获取扩展名,在谷歌上搜索,但没有结果。

The file already exists in a path:

该文件已存在于路径中:

\Storage::get('/uploads/categories/featured_image.jpg);

Now, How can I get the extension of this file above?

现在,我怎样才能得到上面这个文件的扩展名?

Using input fields I can get the extension like this:

使用输入字段我可以得到这样的扩展:

Input::file('thumb')->getClientOriginalExtension();

Thanks.

谢谢。

采纳答案by Jeremy Harris

You can use the pathinfo()function built into PHP for that:

您可以使用PHP 内置的pathinfo()函数:

$info = pathinfo(storage_path().'/uploads/categories/featured_image.jpg');
$ext = $info['extension'];

Or more concisely, you can pass an option get get it directly;

或者更简洁地说,你可以直接传递一个选项 get get ;

$ext = pathinfo(storage_path().'/uploads/categories/featured_image.jpg', PATHINFO_EXTENSION);

回答by Alfredo EM

The Laravel way

Laravel 方式

Try this:

尝试这个:

$foo = \File::extension($filename);

回答by Amir Hossein

Tested in laravel 5.5

在 Laravel 5.5 中测试

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

回答by Daniel Camargo

Yet another way to do it:

另一种方法来做到这一点:

//Where $file is an instance of Illuminate\Http\UploadFile
$extension = $file->getClientOriginalExtension();

回答by aynber

If you just want the extension, you can use pathinfo:

如果您只想要扩展名,可以使用pathinfo

$ext = pathinfo($file_path, PATHINFO_EXTENSION);

回答by BCPNAYAK

 //working code from laravel 5.2

 public function store(Request $request)
 {
          $file = $request->file('file');
            if($file)
            {
                    $extension =  $file->clientExtension();
            }
            echo $extension;
 }

回答by secrethash

Or can use the Extension SplitterTrickster::getExtention()function of https://github.com/secrethash/trickster

或者可以使用https://github.com/secrethash/tricksterExtension SplitterTrickster::getExtention()功能

Trickster::getExtention('some-funny.image.jpg');It returns jpg

Trickster::getExtention('some-funny.image.jpg');它返回 jpg