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 dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multiple upload and rename with cffile

Status
Not open for further replies.

RobHat

Technical User
Nov 30, 2007
91
GB
Hi I am hoping someone here can help me. I am trying to create an insert property page for our real estate website. I am using CF and MSaccess.

I am currently using the following code:

<cfif isdefined("form.submit_upload")>
<cffile action="UPLOAD" filefield="filename1" destination="directpath" nameconflict="MAKEUNIQUE">
<CFSET img1= AgentLOGIN& DateFormat(Now(),"DDMMYY") & TimeFormat(Now(),"HHmmss")>
<CFSET img=ListAppend(img1,FILE.ServerFileExt,".")>
<CFFILE ACTION="RENAME" SOURCE="directpath\#CFFILE.ServerFile#" destination="directpath\#img#">
<cfset form.filename1="#img#">
</cfif>

This is put at the top of the page before the cfinsert code which inserts the details from the form into the db including the name of the file. This works absolutly fine, uploads the image and renames it and then inserts the name into the db.

The problem is I want to allow 3 files to be uploaded at the same time. I have tried just simply duplicating the code with a few changes to names and such .ie #img# is changed to #img2# but it does'nt seem to work. I end up with the first file inserted into the db ok but the file is not renamed and the second file is uploaded and renamed but the coldfusion temporary path is inserted into the db rather than the filename. I am sure I am missing something really simple here or is there an easier way to do this. I have read tons and tons of posts on this subject but can't seem to apply any of the practices to my own application. Any help with this would be most appreciated.

Rob
 
Not quite sure what code you want in particular so I have posted all.

<!--- Filename1 --->
<cfif isdefined("form.submit_upload")>
<cffile action="UPLOAD" filefield="filename1" destination="directpath\" nameconflict="MAKEUNIQUE">
<CFSET img1= AgentLOGIN& DateFormat(Now(),"DDMMYY") & TimeFormat(Now(),"HHmmss")>
<CFSET img=ListAppend(img1,FILE.ServerFileExt,".")>
<CFFILE ACTION="RENAME" SOURCE="directpath\#CFFILE.ServerFile#" destination="directpath\#img#">
<cfset form.filename1="#img#">
</cfif>

<!--- Filename2 --->
<cfif isdefined("form.submit_upload")>
<cffile action="UPLOAD" filefield="filename2" destination="directpath\" nameconflict="MAKEUNIQUE">
<CFSET img2= AgentLOGIN& DateFormat(Now(),"DDMMYY") & TimeFormat(Now(),"HHmmss")>
<CFSET img1=ListAppend(img2,FILE.ServerFileExt,".")>
<CFFILE ACTION="RENAME" SOURCE="directpath\#CFFILE.ServerFile#" destination="directpath\#img1#">
<cfset form.filename1="#img1#">
</cfif>

<!--- Insert Record --->
<cfif IsDefined("FORM.MM_InsertRecord") AND FORM.MM_InsertRecord EQ "upload_property">
<cfquery datasource="My_DataSource">
INSERT INTO MyTable (AgentREF, AgentLOGIN, Type, Location, "Description", Beds, Baths, Euro, Filename1, Filename2, commision_percent, set_fee, commision_comments, date_added, country)
VALUES (<cfif IsDefined("FORM.AgentREF") AND #FORM.AgentREF# NEQ "">
<cfqueryparam value="#FORM.AgentREF#" cfsqltype="cf_sql_clob" maxlength="50">
<cfelse>
''
</cfif>
, <cfif IsDefined("FORM.AgentLOGIN") AND #FORM.AgentLOGIN# NEQ "">
<cfqueryparam value="#FORM.AgentLOGIN#" cfsqltype="cf_sql_clob" maxlength="50">
<cfelse>
''
</cfif>
, <cfif IsDefined("FORM.Type") AND #FORM.Type# NEQ "">
<cfqueryparam value="#FORM.Type#" cfsqltype="cf_sql_clob" maxlength="50">
<cfelse>
''
</cfif>
, <cfif IsDefined("FORM.Location") AND #FORM.Location# NEQ "">
<cfqueryparam value="#FORM.Location#" cfsqltype="cf_sql_clob" maxlength="50">
<cfelse>
''
</cfif>
, <cfif IsDefined("FORM.Description") AND #FORM.Description# NEQ "">
<cfqueryparam value="#FORM.Description#" cfsqltype="cf_sql_clob" maxlength="1073741823">
<cfelse>
''
</cfif>
, <cfif IsDefined("FORM.Beds") AND #FORM.Beds# NEQ "">
<cfqueryparam value="#FORM.Beds#" cfsqltype="cf_sql_clob" maxlength="50">
<cfelse>
''
</cfif>
, <cfif IsDefined("FORM.Baths") AND #FORM.Baths# NEQ "">
<cfqueryparam value="#FORM.Baths#" cfsqltype="cf_sql_numeric">
<cfelse>
NULL
</cfif>
, <cfif IsDefined("FORM.Euro") AND #FORM.Euro# NEQ "">
<cfqueryparam value="#FORM.Euro#" cfsqltype="cf_sql_numeric">
<cfelse>
NULL
</cfif>
, <cfif IsDefined("FORM.filename1") AND #FORM.filename1# NEQ "">
<cfqueryparam value="#FORM.filename1#" cfsqltype="cf_sql_clob" maxlength="250">
<cfelse>
''
</cfif>
, <cfif IsDefined("FORM.filename2") AND #FORM.filename2# NEQ "">
<cfqueryparam value="#FORM.filename2#" cfsqltype="cf_sql_clob" maxlength="250">
<cfelse>
''
</cfif>
, <cfif IsDefined("FORM.commision_percent") AND #FORM.commision_percent# NEQ "">
<cfqueryparam value="#FORM.commision_percent#" cfsqltype="cf_sql_clob" maxlength="50">
<cfelse>
''
</cfif>
, <cfif IsDefined("FORM.set_fee") AND #FORM.set_fee# NEQ "">
<cfqueryparam value="#FORM.set_fee#" cfsqltype="cf_sql_clob" maxlength="50">
<cfelse>
''
</cfif>
, <cfif IsDefined("FORM.commision_comments") AND #FORM.commision_comments# NEQ "">
<cfqueryparam value="#FORM.commision_comments#" cfsqltype="cf_sql_clob" maxlength="1073741823">
<cfelse>
''
</cfif>
, <cfif IsDefined("FORM.date_added") AND #FORM.date_added# NEQ "">
<cfqueryparam value="#FORM.date_added#" cfsqltype="cf_sql_clob" maxlength="50">
<cfelse>
''
</cfif>
, <cfif IsDefined("FORM.country") AND #FORM.country# NEQ "">
<cfqueryparam value="#FORM.country#" cfsqltype="cf_sql_clob" maxlength="50">
<cfelse>
''
</cfif>
)
</cfquery>
<cflocation url="insert.cfm">
</cfif>
<cfparam name="FORM.ID" default="1">

The form is bulk standard at the moment with text fields. It all works fine until I add the code titled filename2 in the upload section. I think the problem has something to do with the fact that I am using the #cffile.serverfile# tag for both therefore there is confusion with which image to rename, but I am not sure what to replace it with or how to lay it out. Oh and I am using this code for the file field:

<input type="file" name="filename1">
<input type="file" name="filename2">

Thanks in advance for any help.
 
<!--- Filename2 --->
...
<cfset form.filename1="#img1#">

The first thing I noticed is you're using overwriting the value of form.filename1 on the second upload. Also, the file names aren't guaranteed to be unique so you could overwrite the file that was uploaded and renamed in step 1

... cfsqltype="cf_sql_clob" maxlength="50">

I don't think you should be using CLOB here. Likely you should be using cf_sql_varchar.

 
Thats great thanks for the help. Works fine now. Sometimes it takes someone else to spot a silly mistake. After this worked the problem I had was that it is was naming the files the same because of the time stamp. The file was being renamed so quick that it ended up with the same name. I changed the code slightly to remedy this.

I set the time stamp before the upload code.

<CFSET today = DateFormat(Now(),"DDMMYY")>
<CFSET now = TimeFormat(Now(),"HHmmss")>

Then I changed the code in the first upload to this:

<CFSET img1= AgentLOGIN& #today# & #now#>

And the second upload to this:

<CFSET img2= AgentLOGIN& #today# & #now# + 1 >

Works like a charm. Adds 1 second onto the time therefore renaming them in sequence.

Again thanks for the help on this, mostly appreciated.

Rob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top