0

I have a laravel 4.1 project running on PHP 5.3.28 and I am trying to find a way to get the file extension from a certain image that fails whenever I use guessExtension() like so:

$format = Input::file('image')->guessExtension();

Every other image I've tried uploading has had no problem using guessExtension(), but it seems Symphony is unable to recognize the mimeType for this specific image:

object(Symfony\Component\HttpFoundation\File\UploadedFile)[9]
  private 'test' => boolean false
  private 'originalName' => string 'herewithme.png' (length=14)
  private 'mimeType' => string 'application/octet-stream' (length=24)
  private 'size' => int 0
  private 'error' => int 1

and my application fails with the following message:

The file "" does not exist

I was thinking about doing something like this:

   $filename = Input::file('image')->getClientOriginalName();

    if(strpos($filename, '.png') !== false)
    {
        $format = 'png';           
    }
    elseif(strpos($filename, '.jpg') !== false)
    {
        $format = 'jpg';            
    }

However, I'm afraid of the random case that a file might cause problems from being named something like pdf.png.jpg (or something to that effect), so I'm hesitant about getting the file format this way. Can anyone think of a better way to do this?

Community
  • 1
  • 1
Fetus
  • 985
  • 2
  • 12
  • 23
  • Maybe just invoke the Linux/Unix `file` command on the uploaded file? – wallyk Oct 01 '14 at 21:17
  • 2
    to get an extension `$ext = pathinfo($filename, PATHINFO_EXTENSION);` but it doesn't solve your problem. I don't know internals of Laravel , but it is possible that maximum upload file size is reached , set in php.ini , may be cause this. Also in your var_dump there is `private 'error' => int 1` can you getErrorMessage or similar on that class object – BojanT Oct 01 '14 at 21:34

2 Answers2

0

You can do it with substr.

You can try $ext = substr($filename,-4);
Aqib1604
  • 292
  • 1
  • 7
-1

Maybe try:

Input::file('image')->getClientMimeType();

or

Input::file('image')->getMimeType();
Jared Eitnier
  • 7,012
  • 12
  • 68
  • 123