Symfony : When to use UploadedFile guessClientExtension() vs guessExtension()?

Uploading a file from a ReactJS/Javascript front end App to a Symfony 4 Backend, I try to get its file extension.

With the file received, I instantiate an UploadedFile object, whichhas the following methods available:

$uploadedFile->getClientOriginalExtension();
$uploadedFile->getClientMimeType();
$uploadedFile->guessClientExtension();
$uploadedFile->guessExtension();

I would expect those getters to return consistent results but it is not really what happens.

Return results for an uploaded file with named image-ile.jpg are :

getClientOriginalExtension() -> ''
getClientMimeType()          -> 'application/octet-stream'
guessClientExtension()       -> 'bin'
guessExtension()             -> 'jpeg'
`

Image file is sent using javascript FormData, with `Content-Type: multipart/form-data` :

> ------WebKitFormBoundaryvFjJSluadmvCob6T
Content-Disposition: form-data; name="image-ile.jpg"; filename="image-ile.jpg"
Content-Type: image/jpeg

My question is: 
In my case, the `guessClientExtension()` doesn't return the relevant file extension while the `guessExtension()` does. So in which cases should I use one getter instead of the other one?

Source: Symfony4 Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *