Solved

Only allow specific file types to be uploaded?

Posted on 2006-07-13
4
230 Views
Last Modified: 2013-12-24
I have a form using an input field to upload an image via CFFILE

the code is like this
<input name="picture" type="file" class="file" id="picture" size="35" value="#url.uploadedfile#" />

How can I use CF to validate that only the requested file types are allowed to be uploaded? I want to limit it to jpg/gif/jpeg ...  

do I do it in the form field, or in the <cffile> function?
what is the syntax?
0
Comment
Question by:MichaelEvangelista
  • 2
  • 2
4 Comments
 
LVL 18

Accepted Solution

by:
Plucka earned 125 total points
ID: 17104878
MichaelEvangelista,

Use the <cffile accept paramater ie

<cffile action="upload" accept="image/jpg, image/gif, image/jpeg"

Regards
Plucka
0
 

Author Comment

by:MichaelEvangelista
ID: 17104945
PERFECT, thanks!

I will accept this answer but I do have  a Part II:
if I try to upload a foreign file type, now I get this error::

//////////////
 The MIME type of the uploaded file "application/octet-stream" was not accepted by the server.

Only files of type "image/jpg, image/gif, image/jpeg" can be uploaded.
Verify that you are uploading a file of the appropriate type.
////////////
Plus the big long eror message, etc.

My upload form already has an error message for the image processing part.
I.e. before adding this code I 'could' upload a foreign file type, but would get a nice formatted error message saying it could not be processed.

Now that I am stopping it from being uploaded in the first place,
I would like to somehow connect to that same error message.

It is called with

<cfcase value="notanimage">
<cfset userMessage = "File unable to be processed.<br>(Could be corrupt or using unknown compression.)<br>Please upload a valid picture file (jpg, gif, png.)" />
<cfset userMessageType = "notice" />
</cfcase>

How can I cause rejection of the filetype by the CFFILE accept parameter
to return to the page with that CFCASE set, rather than throwing the default CF error for the wrong file type?

I will gladly add points to this discussion... or however that works... let me know...


** Is there some way I can "catch" this error and
0
 
LVL 18

Expert Comment

by:Plucka
ID: 17104973
Yes use try/catch block

<cftry>
    <cffile action="upload"

    <cfcatch type="any">
        An error occured
    </cfcatch>
</cftry>
0
 

Author Comment

by:MichaelEvangelista
ID: 17105058
PERFECT !!! Thanks so much!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This is a guide to setting up a new WHM/cPanel Server to be used for web hosting accounts. It is intended for web hosting company administrators and dedicated server owners. For under $99 per month (considering normal rate of Big Data Cetnters like …
Most ColdFusion developers get confused between the CFSet, Duplicate, and Structcopy methods of copying a Structure, especially which one to use when. This Article will explain the differences in the approaches with examples; therefore, after readin…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now