Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I am uploading a file name from a f 1

Status
Not open for further replies.

izachar

Technical User
Oct 7, 2000
84
CA
I am uploading a file name from a form but I need to change the file name and store the new file in the db so I can recall it later. The problem I have is with the extensions. I tried this:

<cfif isdefined(p_picture) contains &quot;.gif&quot;>
<cfset picture=&quot;pic&quot;&#p_id#&&quot;.gif&quot;>
<cfelse>
<cfset picture=&quot;pic&quot;&#p_id#&&quot;.jpg&quot;>
</cfif>

It will take only he second option regardless of the extension I put.
 
The problem is here:
<cfif isdefined(p_picture) contains &quot;.gif&quot;>


isdefined will never contain &quot;.gif&quot;. What you meant to put, I think, was:

<cfif isdefined(p_picture)>
<cfif p_picture contains &quot;.gif&quot;>

</cfif>
</cfif> John Hoarty
jhoarty@quickestore.com
 
Thank you for the responce but it is still not working. I tried an utput on the P_picture variable and it is giving me C:\WINNT\TEMP\ACF21.tmp which does not make sence. This is what I have now:


<cfif isdefined(&quot;p_picture&quot;)>
<cfif #p_picture# contains &quot;.gif&quot;>
<cfset picture=&quot;pic&quot;&#p_id#&&quot;.gif&quot;>
<cfelse>
<cfset picture=&quot;pic&quot;&#p_id#&&quot;.jpg&quot;>
</cfif>
</cfif>
 
You should really use getfilefrompath and getdirectoryfrompath, as well as #form.p_picture# to ensure that everything is straight. Here's some helpful code straight out of Allaire's documentation:

<CFSET thisPath= ExpandPath(&quot;*.*&quot;)>
<CFSET thisDirectory= GetDirectoryFromPath(thisPath)>
<CFOUTPUT>
The current directory is: #GetDirectoryFromPath(thisPath)#
<CFIF IsDefined(&quot;FORM.yourFile&quot;)>
<CFIF FORM.yourFile CONTAINS &quot;.gif&quot;> <!--- my edit <g> --->
<CFSET yourFile = FORM.yourFile>
<CFIF FileExists(ExpandPath(yourfile))>
<P>Your file exists in this directory. You entered
the correct file name, #GetFileFromPath(&quot;#thisPath#/#yourfile#&quot;)#
<CFELSE>
blah, blah
</CFIF>

</CFIF>
</CFIF>

You get the idea. This is really the best way to work with that troublesome CFFILE tag. Good luck.
John Hoarty
jhoarty@quickestore.com
 
You need to upload the image first to a directory before you can rename it and check the extension.

Something like this:

<form action=&quot;test.cfm&quot; method=&quot;post&quot; ENCTYPE=&quot;multipart/form-data&quot;>
<input TYPE=&quot;File&quot; name=&quot;p_picture&quot;><BR>
<input type=&quot;Submit&quot;>
</form>


<CFSET thisPath= ExpandPath(&quot;*.*&quot;)>
<CFSET imgDirectory= GetDirectoryFromPath(thisPath)>

<CFIF IsDefined(&quot;FORM.p_picture&quot;)>
<CFTRY>
<!--- Upoad File --->
<CFFILE ACTION=&quot;UPLOAD&quot;
FILEFIELD=&quot;FORM.p_picture&quot;
DESTINATION=&quot;#imgDirectory#&quot;
NAMECONFLICT=&quot;Overwrite&quot;
ACCEPT=&quot;image/pjpeg,image/jpeg,image/gif&quot;>


<!--- Rename Img --->
<CFFILE ACTION=&quot;RENAME&quot;
SOURCE=&quot;#CFFILE.ServerDirectory#\#CFFILE.ServerFile#&quot;
DESTINATION=&quot;#CFFILE.ServerDirectory#\pic#p_id#.#CFFILE.ServerFileExt#&quot;>


<!--- Catch error, if it's not a jpg or gif file --->
<CFCATCH TYPE=&quot;Any&quot;>
I'm sorry, you must upload a .gif or .jpg file.
</CFCATCH>
</CFTRY>
</CFIF> - tleish
 
Hi! I'm having a similar problem as izachar. I don't think this is a CF problem. I have a form similar to tleish:
<form action=&quot;test.cfm&quot; method=&quot;post&quot; ENCTYPE=&quot;multipart/form-data&quot;>
<input TYPE=&quot;File&quot; name=&quot;p_picture&quot;><BR>
<input type=&quot;Submit&quot;>
</form>


In the action file, when I print out the filename, it is a .tmp file like izachar's. I tried the code by tleish and it didn't work even when I attempted to upload a .gif file because the filename is already a .tmp file by that time.

I have an application that prompts the user for a filename. I want to use cfmail to mail a message with that file attached. Is there any way to preserve the user's original filename? For example, when you email someone and attach a filename, the recipient gets THAT filename attached to the mail message. It is not renamed. When I run my code, browse and select a file c:/My Documents/testfile.doc. I want my mail message to contain the attachment testfile.doc. I hope this makes sense and thanks!!
 
Snix,

The problem is, the server put's it into a tmp file even in the form value until it knows where to put, at which it then recognizes it as the correct name. The way to dynamically get that name is to first upload it to a directory using CFFILE, then you can dynamically get it's name using CFFILE.ServerFile and the direcory where you uploaded it to CFFILE.ServerDirectory. Using these variables you can then upload the file, attatch it to the email, send the email, and delete the file.

The script below would work anywhere on a server running ColdFusion. All you would need to do is change the FORM and/or the CFMAIL settings (accept for the MIMEATTACH property).

[COLOR=000080][COLOR=FF8000]<form action=&quot;test.cfm&quot; method=&quot;post&quot; ENCTYPE=&quot;multipart/form-data&quot;>[/color][/color]
[COLOR=000080][COLOR=FF8000]<input TYPE=&quot;File&quot; name=&quot;attatchment&quot;>[/color][/color][COLOR=000080]<BR>[/color]
[COLOR=000080][COLOR=FF8000]<input type=&quot;Submit&quot; VALUE=&quot;Send Email&quot;>[/color][/color]
[COLOR=000080][COLOR=FF8000]</form>[/color][/color]


<CFSET thisPath= ExpandPath(&quot;*.*&quot;)>
<CFSET thisDirectory= GetDirectoryFromPath(thisPath)>

<CFIF IsDefined(&quot;FORM.attatchment&quot;)>
<CFTRY>
[COLOR=666666]<!--- Upoad File --->[/color]
<CFFILE ACTION=&quot;UPLOAD&quot;
FILEFIELD=&quot;FORM.attatchment&quot;
DESTINATION=&quot;#thisDirectory#&quot;
NAMECONFLICT=&quot;Overwrite&quot;>


[COLOR=666666]<!--- Send Email --->[/color]
<CFMAIL FROM=&quot;from@email.com&quot; TO=&quot;to@email.com&quot;
SUBJECT=&quot;This is a test&quot;
MIMEATTACH=&quot;#CFFILE.ServerDirectory#\#CFFILE.ServerFile#&quot;>

Take a look at this attatchment.
</CFMAIL>

[COLOR=666666]<!--- Delete Attatchment --->[/color]
<CFFILE ACTION=&quot;DELETE&quot;
FILE=&quot;#CFFILE.ServerDirectory#\#CFFILE.ServerFile#&quot;>


[COLOR=666666]<!--- Catch error, if it's not a jpg or gif file --->[/color]
<CFCATCH TYPE=&quot;Any&quot;>
You have encountered an error sending this email, please try it again.
</CFCATCH>
</CFTRY>
</CFIF> - tleish
 
Thanks so much tleish!! I have the filename now. However, one question: I only seem to get the attachment when I don't do the delete. Could the file be deleted before it's attached somehow?? Thanks again.
 
I was successfully able to do it, but I did wonder if there was a chance that the file could be deleted before the email was sent.

First comment the delete out to make sure that the file is being attatched without the delete present to determine if that actually is the problem.

Another thing you could try is to put a CFLOCK on the file when it's being emailed and when it's deleted so that it can't be deleted while the email function is running. I don't know if that would solve this particular problem, but it's worth a try:

[COLOR=666666]<!--- Send Email --->[/color]
<CFLOCK NAME=&quot;#CFFILE.ServerDirectory#\#CFFILE.ServerFile#&quot; TIMEOUT=60 TYPE=&quot;Exclusive&quot;>
<CFMAIL FROM=&quot;from@email.com&quot; TO=&quot;to@email.com&quot;
SUBJECT=&quot;This is a test&quot;
MIMEATTACH=&quot;#CFFILE.ServerDirectory#\#CFFILE.ServerFile#&quot;>

Take a look at this attatchment.
</CFMAIL>
</CFLOCK>

[COLOR=666666]<!--- Delete Attatchment --->[/color]
<CFLOCK NAME=&quot;#CFFILE.ServerDirectory#\#CFFILE.ServerFile#&quot; TIMEOUT=60 TYPE=&quot;Exclusive&quot;>
<CFFILE ACTION=&quot;DELETE&quot;
FILE=&quot;#CFFILE.ServerDirectory#\#CFFILE.ServerFile#&quot;>

</CFLOCK>

You may want to come up with a different method of deleting files on the server at a later time, like scheduling a script it the CF Administrator that takes a directory a deletes all files in there. - tleish
 
Thanks tleish! I think the file is being deleted before it is mailed! I did comment out the delete and it worked great!! Then I put a little <cfquery> between the cfmail and the cffile to delete. This worked and the file was attached. I will try you other suggestions, especially cflock. Thanks for all your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top