Go Back   SitePoint Forums > Forum Index > Program Your Site > ColdFusion
Newsletter FAQ Members List Calendar Mark Forums Read

New to SitePoint Forums? Register here for free!

SitePoint Sponsor
 
Reply
 
Thread Tools Display Modes
Old Apr 14, 2004, 16:20   #1
mdumka
SitePoint Guru
 
mdumka's Avatar
 
Join Date: Jul 2003
Location: True North
Posts: 625
Uploading Images In ColdFusion

I would like a little help from you fine folks here

I know how to upload images, rename them and place them in directorys.

What I would like to is befor I upload them Check thier extension, file size and dimernsions in that order of importance.

Is this possible?

I have been searching but with no luck,

Thanks
mdumka is offline   Reply With Quote
Old Apr 15, 2004, 07:56   #2
Rynoguill
minister of propaganda
silver trophy
 
Rynoguill's Avatar
 
Join Date: Feb 2004
Location: Midsouth
Posts: 1,400
youre talking about mime types, i dont use them much so i cant tell you what you need off the top of my head. i think davidjmedlock has helped me with something like this before, hopefully he will stop by here soon.

if i can come up with that previous post ill let you know, i cant seem to find it so far though.
Rynoguill is offline   Reply With Quote
Old Apr 15, 2004, 07:58   #3
Rynoguill
minister of propaganda
silver trophy
 
Rynoguill's Avatar
 
Join Date: Feb 2004
Location: Midsouth
Posts: 1,400
found it, check this thread out.

http://www.sitepoint.com/forums/show...highlight=mime
Rynoguill is offline   Reply With Quote
Old Apr 15, 2004, 09:45   #4
jonese
SitePoint Wizard
 
jonese's Avatar
 
Join Date: Jul 1999
Location: Powder Springs GA (Atlanta)
Posts: 1,344
sounds like you are dealing with images (dimension?) have you looked into image Magik? there's a cool CF tag at http://www.alagad.com/index.cfm/name-mtinfo and it's free!
jonese is offline   Reply With Quote
Old Apr 15, 2004, 09:50   #5
mdumka
SitePoint Guru
 
mdumka's Avatar
 
Join Date: Jul 2003
Location: True North
Posts: 625
Thanks buddy, I dont want to change the file, just want to restrict them from uploading anything but .jpg.

I think I am going to try and use some error handling using the file .extensions.

Later
mdumka is offline   Reply With Quote
Old Apr 15, 2004, 09:50   #6
davidjmedlock
SitePoint Wizard
 
davidjmedlock's Avatar
 
Join Date: Dec 2002
Location: Nashville, TN USA
Posts: 2,039
Quote:
Originally Posted by jonese
sounds like you are dealing with images (dimension?) have you looked into image Magik? there's a cool CF tag at http://www.alagad.com/index.cfm/name-mtinfo and it's free!
I believe that requires the ImageMagik program to be installed on the server. Some shared hosts don't like doing that kind of thing, though they may work with you if you threaten to take your business elsewhere...

Here's an article on File Uploads:

http://www.kodefusion.com/article/in...d&ArticleID=13

To check for image MIME types, you want to use the "accept" attribute of CFFILE. You would enter a comma delimited list of MIME types you're willing to allow users to upload and then ColdFusion would automatically accept or reject it based on the file type. For images you might specify "image/jpeg,image/pjpeg,image/gif,image/png,image/bmp". You might be able to specify "image/*", but I'm not 100% sure.

Also, look at my latest article on SitePoint about using HTTP with ColdFusion. There's some code in there for a simple Image server that you might find useful.
davidjmedlock is offline   Reply With Quote
Old Apr 16, 2004, 09:40   #7
mdumka
SitePoint Guru
 
mdumka's Avatar
 
Join Date: Jul 2003
Location: True North
Posts: 625
Well What I did is download the imageinfo custom tag from Macromedia (it is free) to give the information of file type, file size and dimensions.

I know File.ServerFile and other functions would have got me the extension and size fo the file but no dimensions so I am using this.

My logic is 2 upload to a temp directory, check the the file to see if it fits my rule for size, dimensions and ext. If it does, rename it and upload it to my directory, if it does not, store the value of size, dimensions and ext in a variable, delete the temp file and display the error to the user.

Does this sound like the right logic to use?

Thanks

Mike
mdumka is offline   Reply With Quote
Old Apr 16, 2004, 13:35   #8
creole
SitePoint Wizard
 
creole's Avatar
 
Join Date: Oct 2000
Location: Nashvegas Baby!
Posts: 7,991
mdumka...

Care to share that custom tag?
creole is offline   Reply With Quote
Old Apr 16, 2004, 13:37   #9
davidjmedlock
SitePoint Wizard
 
davidjmedlock's Avatar
 
Join Date: Dec 2002
Location: Nashville, TN USA
Posts: 2,039
Quote:
Originally Posted by mdumka
Well What I did is download the imageinfo custom tag from Macromedia (it is free) to give the information of file type, file size and dimensions.

I know File.ServerFile and other functions would have got me the extension and size fo the file but no dimensions so I am using this.

My logic is 2 upload to a temp directory, check the the file to see if it fits my rule for size, dimensions and ext. If it does, rename it and upload it to my directory, if it does not, store the value of size, dimensions and ext in a variable, delete the temp file and display the error to the user.

Does this sound like the right logic to use?

Thanks

Mike
Yeah, thats about right. I think that's pretty much exactly what I did on ezImageCenter.us. Just be sure to use the accept attribute to keep someone from uploading an exe and blowing up your app or something...
davidjmedlock is offline   Reply With Quote
Old Apr 17, 2004, 10:35   #10
mdumka
SitePoint Guru
 
mdumka's Avatar
 
Join Date: Jul 2003
Location: True North
Posts: 625
Thanks guys, will up the tag on monday!

Later
mdumka is offline   Reply With Quote
Old Apr 19, 2004, 09:31   #11
mdumka
SitePoint Guru
 
mdumka's Avatar
 
Join Date: Jul 2003
Location: True North
Posts: 625
Here is the ImageInfo custom tag.

Later
Attached Files
File Type: zip ImageInfo.zip (3.4 KB, 73 views)
mdumka is offline   Reply With Quote
Old Apr 19, 2004, 09:32   #12
mdumka
SitePoint Guru
 
mdumka's Avatar
 
Join Date: Jul 2003
Location: True North
Posts: 625
why is the upload not showing?

If I go to edit my post it shows it uploaded but not in my post. Help?
mdumka is offline   Reply With Quote
Old Apr 19, 2004, 09:37   #13
creole
SitePoint Wizard
 
creole's Avatar
 
Join Date: Oct 2000
Location: Nashvegas Baby!
Posts: 7,991
It hasn't been approved yet. An admin has to look it over before it gets made public.
creole is offline   Reply With Quote
Old Apr 19, 2004, 09:51   #14
mdumka
SitePoint Guru
 
mdumka's Avatar
 
Join Date: Jul 2003
Location: True North
Posts: 625
Quote:
Originally Posted by creole
It hasn't been approved yet. An admin has to look it over before it gets made public.
Ok, thanks buddy. Well when it is given the A-OK the tag is there for ya.

Later
mdumka is offline   Reply With Quote
Old Apr 19, 2004, 10:19   #15
creole
SitePoint Wizard
 
creole's Avatar
 
Join Date: Oct 2000
Location: Nashvegas Baby!
Posts: 7,991
Great...can't wait to get it.
creole is offline   Reply With Quote
Old Apr 27, 2004, 10:56   #16
lostfox
SitePoint Member
 
lostfox's Avatar
 
Join Date: Apr 2004
Location: Raleigh, NC
Posts: 15
there may be a better way but here's how i do it
i actually upload the file, check its extension and delete it if its not an acceptable image file(gif and .jpg)

<cfif #cffile.ClientFileExt# EQ 'gif' OR #cffile.ClientFileExt# EQ 'jpg' OR #cffile.ClientFileExt# EQ 'pjpeg' OR #cffile.ClientFileExt# EQ 'jpeg' OR #cffile.ClientFileExt# EQ 'pjpg'>

i havn't had any speed issues with this process yet. if anybody has a better idea, i could use this tip too
lostfox is offline   Reply With Quote
Old Apr 27, 2004, 11:52   #17
mdumka
SitePoint Guru
 
mdumka's Avatar
 
Join Date: Jul 2003
Location: True North
Posts: 625
but how do you check dimensions?

I ask this becuase sometimes I have to ensure my users do not upload the wrong size images.
mdumka is offline   Reply With Quote
Old Apr 27, 2004, 12:01   #18
creole
SitePoint Wizard
 
creole's Avatar
 
Join Date: Oct 2000
Location: Nashvegas Baby!
Posts: 7,991
lostfox...

Why take the time to make that check when you can simply prevent users from uploading a file type that you don't want to begin with? Like so:
PHP Code:

<cffile

action
="upload"
fileField="uploadbanner"
destination="#uploadPath#"
nameConflict="MakeUnique"
accept="image/gif,image/jpeg,image/pjpeg">
creole is offline   Reply With Quote
Old Apr 27, 2004, 22:07   #19
lostfox
SitePoint Member
 
lostfox's Avatar
 
Join Date: Apr 2004
Location: Raleigh, NC
Posts: 15
PHP:

<cffile
action="upload"
fileField="uploadbanner"
destination="#uploadPath#"
nameConflict="MakeUnique"
accept="image/gif,image/jpeg,image/pjpeg">

i do it that my way because if they try to upload file with an unacceptable extension, the user gets a coldfusion error. with the way i do it, i can print out a "hey you tried to upload an unacceptable file" message to them along with instructions of what to do next. i don't know how common this is, but i try to keep the user from getting the coldfusion errors
lostfox is offline   Reply With Quote
Old Apr 28, 2004, 02:59   #20
duncancumming
SitePoint Member
 
Join Date: Apr 2004
Location: Glasgow, Scotland, UK
Posts: 2
Quote:
Originally Posted by lostfox
i do it that my way because if they try to upload file with an unacceptable extension, the user gets a coldfusion error. with the way i do it, i can print out a "hey you tried to upload an unacceptable file" message to them along with instructions of what to do next. i don't know how common this is, but i try to keep the user from getting the coldfusion errors
wouldn't you be better to put the <cffile> in a <cftry> block, then <cfcatch> the error?
duncancumming is offline   Reply With Quote
Old Apr 28, 2004, 07:14   #21
Rynoguill
minister of propaganda
silver trophy
 
Rynoguill's Avatar
 
Join Date: Feb 2004
Location: Midsouth
Posts: 1,400
Quote:
Originally Posted by duncancumming
wouldn't you be better to put the <cffile> in a <cftry> block, then <cfcatch> the error?
you really need to use the cftry and catch blocks around things like that, its a much better way to do it. for you and the user. and, i dont know if this is true in your case, but if youre working with files that could possibly be used at the same time (or try to be), or if two people my be trying to upload pictures with the same name at the same time, you need to use locks around the cffile as well. its called race conditions, and i think ben forta's blog has some more info about it. you may actually want to get in the cf documentation too though.
Rynoguill is offline   Reply With Quote
Old Apr 28, 2004, 08:22   #22
creole
SitePoint Wizard
 
creole's Avatar
 
Join Date: Oct 2000
Location: Nashvegas Baby!
Posts: 7,991
Good point Lostfox...thanks for clarifying.
creole is offline   Reply With Quote
Old Apr 28, 2004, 16:18   #23
lostfox
SitePoint Member
 
lostfox's Avatar
 
Join Date: Apr 2004
Location: Raleigh, NC
Posts: 15
The next time i have the chance, i will give the cftry and cfcatch a go. that is the first that i've heard of it. when i started the project in which used the above code, i did a quick look into a try/catch type of functions but quickly gave up and decided i could write the code to upload and delete it just as easily.

thanks for the tip.
lostfox is offline   Reply With Quote
Old Apr 29, 2004, 07:21   #24
Rynoguill
minister of propaganda
silver trophy
 
Rynoguill's Avatar
 
Join Date: Feb 2004
Location: Midsouth
Posts: 1,400
Quote:
Originally Posted by lostfox
The next time i have the chance, i will give the cftry and cfcatch a go. that is the first that i've heard of it. when i started the project in which used the above code, i did a quick look into a try/catch type of functions but quickly gave up and decided i could write the code to upload and delete it just as easily.

thanks for the tip.
yes definately look into it. its not hard, im sure youll pick it up quite easily, and it really has a lot of power if youre interested in tapping into it. let us know if you have any trouble with it.
Rynoguill is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread | Next Thread »

Thread Tools
Display Modes

 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Sponsored Links
 
Forum Jump


All times are GMT -7. The time now is 07:31.


Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Copyright 1998-2009, SitePoint Pty Ltd. All Rights Reserved