Solved

Only allow specific file types to be uploaded?

Posted on 2006-07-13
4
237 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 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

Don't Miss ATEN at InfoComm 2017!

Visit booth #2167 to see the  new ATEN VM3200 32 x 32 Modular Matrix Switch. Other highlights include the VE8950 4K HDMI Over IP Extender, VS1912 12-Port DP Video Wall Media Player  and VK2100 ATEN Control System. Register now with Free Pass Code ATEN288!

Question has a verified solution.

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

In our day to day coding, how many times have we come across a necessity to check whether a URL is a broken link or not? For those of you that answered countless and are using ColdFusion like myself, then this article is for you.  It will show yo…
One of the typical problems I have experienced is when you have to move a web server from one hosting site to another. You normally prepare all on the new host, transfer the site, change DNS and cross your fingers hoping all will be ok on new server…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

726 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