?
Solved

Only allow specific file types to be uploaded?

Posted on 2006-07-13
4
Medium Priority
?
244 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 18

Accepted Solution

by:
Plucka earned 500 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever sent email via ColdFusion and thought of tracking this mail to capture the exact date and time when the message was opened ?  If yes, then this article is for you ! First we need a table user_email with columns user_id , email , sub…
Meet the world's only “Transparent Cloud™” from Superb Internet Corporation. Now, you can experience firsthand a cloud platform that consistently outperforms Amazon Web Services (AWS), IBM’s Softlayer, and Microsoft’s Azure when it comes to CPU and …
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

650 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