Skip to content

[7.x] File::guessedExtension() method - #33001

Merged
taylorotwell merged 1 commit into
laravel:7.xfrom
hivokas:guessed-extension
May 29, 2020
Merged

[7.x] File::guessedExtension() method#33001
taylorotwell merged 1 commit into
laravel:7.xfrom
hivokas:guessed-extension

Conversation

@hivokas

@hivokas hivokas commented May 28, 2020

Copy link
Copy Markdown
Contributor

This PR provides a File::guessedExtension() method which could be useful in case we want to know the extension of the file which actually doesn't have one.

>>> File::name(public_path('image.png'))
=> "image"
>>> File::extension(public_path('image.png'))
=> "png"
>>> File::guessedExtension(public_path('image.png'))
=> "png"
>>> File::copy(public_path('image.png'), public_path('image'))
=> true
>>> File::name(public_path('image'))
=> "image"
>>> File::extension(public_path('image'))
=> ""
>>> File::guessedExtension(public_path('image'))
=> "png"

Under the hood, it guesses the extension by the mime-type.

It this PR gets approved, I'll provide tests.

@driesvints

Copy link
Copy Markdown
Member
>>> File::guessedExtension(public_path('image.png'))
=> "png"

But your last example actually has an extension? I think that defeats the purpose?

@hivokas

hivokas commented May 28, 2020

Copy link
Copy Markdown
Contributor Author

Sorry, that was a typo.
I updated the description.

@bonzai

bonzai commented May 28, 2020

Copy link
Copy Markdown
Contributor

This will not work if I use Filesystem component outside of Laravel, because symfony/mime package is missing in composer.json:

https://github.com/laravel/framework/blob/7.x/src/Illuminate/Filesystem/composer.json

@GrahamCampbell

Copy link
Copy Markdown
Collaborator

Don't we already have a method for guessing extensions in the framework?

@GrahamCampbell

Copy link
Copy Markdown
Collaborator

https://github.com/laravel/framework/blob/v7.13.0/src/Illuminate/Http/Testing/MimeType.php

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants