Tool
Tool

Reputation: 12488

Symfony 2 file upload: guessExtension() doesn't work for .docx files

public function preUpload()
{
    if (null !== $this->file) {
        $this->path = $this->file->guessExtension();

    }
}

This doesn't work for .docx files.

I get a file stored under the name "myfile." -> no extension.

How to handle this?

Upvotes: 3

Views: 11709

Answers (3)

Tortus
Tortus

Reputation: 151

I found a bug in symfony core files where It was missing a mimeType for the .xls files.

We had the same behaviour: GuessExtension would return null.

My team and I narrowed it down to an array which lists symfony's mime types.

Here is a link to the same solution I answered on another question: https://stackoverflow.com/a/36435844/3980097

You will find the exact path to MimeTypeExtensionGuesse.php

In your case, the missing mime Type might be:

'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'docx',

I hope this helps!

Upvotes: 1

keyboardSmasher
keyboardSmasher

Reputation: 2811

use

 getExtension()

as a fallback?

Symfony API

Upvotes: 3

Quique Torras
Quique Torras

Reputation: 230

I think you must use:

getClientOriginalExtension()

because you want to get the extension of the original name not the temporary name that the file has in your server.

http://api.symfony.com/2.4/Symfony/Component/HttpFoundation/File/UploadedFile.html

Upvotes: 13

Related Questions