• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Get mimetype of file object

LEGEND ,
May 30, 2016 May 30, 2016

Copy link to clipboard

Copied

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,

^_^

Views

1.4K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , May 30, 2016 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 f

...

Votes

Translate

Translate
LEGEND ,
May 30, 2016 May 30, 2016

Copy link to clipboard

Copied

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. 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
May 31, 2016 May 31, 2016

Copy link to clipboard

Copied

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.)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 31, 2016 May 31, 2016

Copy link to clipboard

Copied

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,

^_^

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
May 31, 2016 May 31, 2016

Copy link to clipboard

Copied

LATEST

If "strict=true", you may encounter problems.  It looks like EXE files w/JPG extensions are incorrectly identified as JPGs. (The bug status is still "open/ToFix".)
Bug#4130274 - FileGetMimeType()'s strict does not agree with fileupload()'s strict

Since you are testing image uploads, using isImage() is recommended, but I've found that this isn't 100% either as CF10 failed to detect an iPhone4 JPEG photo as a valid photo.  Your mileage may vary w/CMYK images too as CF takes longer to process them.  Our upload process for image files has been to upload, identify extension, validate image using CF_GraphicsMagick (faster, supports more image variations) and sanitize w/CF_GraphicsMagick (remove metadata, convert to RGB JPG, resize if necessary, etc).

We don't save images to the database as it would add more volume to back-end database requests. We prefer the file system so that it can be synced across other servers. We do save the metadata (format, dimensions, gps, etc) and file's physical location in the database.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation