Skip to main content
WolfShade
Legend
May 30, 2016
Answered

Get mimetype of file object

  • May 30, 2016
  • 1 reply
  • 1729 views

Hello, all,

This side project I'm working on is really pushing me to learn some new things (which I enjoy!); but I keep running into issues that _should_ be pretty simple to figure out.

This time, I need to detect the mimetype of an uploaded file _without_ saving it to the hard drive, and _not_ by the file extension.

CF11 on an Ubuntu Server (Trusty Tahr 14.04.4 LTS; no GUI, it's all CLI)

The process that I have has been mentioned in an earlier post in this forum:  image(s) are uploaded to the server without being saved to the HD, and the system loops through each of the .tmp files to insert the binaries of the file(s) into an array so that I can insert them directly into a database.

But I would like to limit the acceptable mimetypes to strictly .gif, .jpg, and .jpeg.

Is there a way, as I loop through the .tmp files, to detect the mimetype so I can reject anything that isn't .gif, or .jpg/.jpeg?  The binaries are first loaded into a variable and I'm using isImage() to make sure that it _is_ an image.

V/r,

^_^

    This topic has been closed for replies.
    Correct answer WolfShade

    Hello, all,

    I think I may have found a solution.  Wish I had found it before asking in the forums (I really did try, first, honest).  Sorry if I've wasted anyone's time.

    <cfloop index="idx" list="#form.images#">

        <cfscript>

            mime = fileGetMimeType(idx);

            if(NOT FindNoCase('gif',idx) AND NOT FindNoCase('jpg',idx) AND NOT FindNoCase('jpeg',idx){

                ListDeleteAt(form.images,ListFind(idx));

                }

        </cfscript>

    </cfloop>

    This will remove reference to the .tmp file from the form object that contains multiple files IF it does not match gif, jpg, or jpeg.  Removing the reference means that the file will be deleted since no action will be taken for it. 

    V/r,

    ^_^

    PS:  If you can think of a better way to do this, I am all about learning new things. 

    1 reply

    WolfShade
    WolfShadeAuthorCorrect answer
    Legend
    May 30, 2016

    Hello, all,

    I think I may have found a solution.  Wish I had found it before asking in the forums (I really did try, first, honest).  Sorry if I've wasted anyone's time.

    <cfloop index="idx" list="#form.images#">

        <cfscript>

            mime = fileGetMimeType(idx);

            if(NOT FindNoCase('gif',idx) AND NOT FindNoCase('jpg',idx) AND NOT FindNoCase('jpeg',idx){

                ListDeleteAt(form.images,ListFind(idx));

                }

        </cfscript>

    </cfloop>

    This will remove reference to the .tmp file from the form object that contains multiple files IF it does not match gif, jpg, or jpeg.  Removing the reference means that the file will be deleted since no action will be taken for it. 

    V/r,

    ^_^

    PS:  If you can think of a better way to do this, I am all about learning new things. 

    James Moberg
    Inspiring
    May 31, 2016

    The uploaded file's mime type is specified by the browser uploading the file.  We've had some issues with the mime type not being what we expected be due to third-party browser plugins.  You could try using Ryan Stille's getClientFileName UDF:

    Stillnet Studios » Blog Archive » Getting the client filename before using cffile

    This method will provide you with the "file extension" rather than the mime type.  (You may have to add "jpeg" to the list of allowable extensions to support JPG and then rename the file on-the-fly.)

    WolfShade
    WolfShadeAuthor
    Legend
    May 31, 2016

    It's my understanding that fileGetMimeType() will use the file extension if the strict argument is set to false.

    The default is true, which uses the first few bits of the file to determine the mimetype.  It was introduced in CF10.

    V/r,

    ^_^