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

CFFile Upload

Status
Not open for further replies.

dmxd99

Programmer
Jul 19, 2005
20
0
0
US
I can upload my files (pictures) to the webserver without any problem. However, if I try to upload a few more files, they just overwrite my previous files even though their names are unique. Anyone has any suggestions?
 
It sounds like you have nameConflict="overwrite" in your upload code. Try changing it to nameConflict="MakeUnique"

Hope this helps


Wullie

Fresh Look - Quality Coldfusion 7/Windows Hosting

The pessimist complains about the wind. The optimist expects it to change. The leader adjusts the sails. - John Maxwell
 
I tried them all (makeunique,error etc) even the file names have distinct names, but still have the same problem.
 
This is my form page

<head>
<style type="text/css">
p.normal {font-weight: normal;
font-size: 80%}
p.thick {font-weight: bold}
</style>
</head>

<cfoutput>
<cfset PixNum = 10>
<cfif #attributes.Type# EQ "F" >
<p class="normal">Name of the link</p>
<!--- For new link --->
<input type="text"
name="LinkName"
required="yes">
</cfif>

<input type="hidden"
name="PixNum"
value="#PixNum#">

<input type="hidden"
name="Type"
value="#attributes.Type#">

<!--- For existing link --->
<input type="hidden"
name="ID"
value="#attributes.ID#">


<cfset Path = "Path">
<cfset Desc = "Desc">

<cfloop from="1" to="#variables.PixNum#" index="i">
<cfset FileName = "File" & #i#>
<cfset Desc = "Desc" & #i#>
<p class="normal">Location of the image #i#</p>
<!--- browse button --->
<input type="File"
name="#variables.FileName#"
required="yes">

<!--- Description of each Image --->
<p class="normal">Enter description for the image </p>
<textarea name="#variables.Desc#"
cols="30"
rows="3"
title="Enter description">
</textarea> <br> <br> <hr>
</cfloop>
</cfoutput>========================================================
This is my Action Page

<cfset count = 0>
<cfset PixID = 0>
<cfset Path = "C:\CFusionMX7\
<cfoutput>
<cfloop from="1" to="#attributes.PixNum#" index="i" step="1">
<cfset FileName = "Form.File" & #i#>
<cfset Desc = "Form.Desc" & #i#>
<cfset Desc = evaluate(variables.Desc)>

<cfif evaluate(variables.FileName) NEQ "">
<cffile action="upload"
filefield="#variables.FileName#"
destination="#Path#"
ACCEPT="image/jpg, image/pjpeg,image/gif, application/msword, image/bmp, image/tiff"
nameconflict="OVERWRITE">
<!--- Store name of the uploaded file --->
<cfset Count = Count + 1>
<cfset ImageName = #Trim(file.serverfile)#>
<cfset Ext = #Mid(ImageName, Len(ImageName)-2, 3)# >
<cfset ParentID = #Trim(Form.ID)#>

<!--- Insert a link/item into TreeTable, only do it once--->
<cfif #Form.Type# EQ "F">
<!--- At the Folder level, need create a new link in TreeTable --->
<cfset Link = #Trim(Form.LinkName)#>
<cfif Count EQ 1>
<cfquery name="InsertLink" datasource="TreeDS">
Insert into TreeTable
values ('#Link#','L',#ParentID#)
</cfquery>
</cfif>
<!--- Find the auto generated ItemID after inserting link from the above query --->
<cfquery name="FindLinkID" datasource="TreeDS">
SELECT ItemID
FROM TreeTable
WHERE ItemName = '#Link#' AND
ParentID = #ParentID#
</cfquery>
<cfset LinkIDCol = #FindLinkID.ItemID#>

<cfelseif #Form.Type# EQ "L">
<!--- At the Link level, no need to create new link, use existing link to add images --->
<cfset LinkIDCol = #Form.ID#>
</cfif>

<!--- Insert Image to ContentTable --->
<cfquery name="InsertImage" datasource="TreeDS">
Insert into ContentTable
values ('#Ext#', #LinkIDCol#, '#Desc#')
</cfquery>

<!--- Rename images after the ImageID --->
<cfquery name="FindImageID" datasource="TreeDS">
Select ID
From ContentTable
Where LinkID= #LinkIDCol#
</cfquery>

<cfset ImageID = #FindImageID.ID# + #PixID#>
<cfset Src = #Path# & #ImageName#>
<cfset Dest = #Path# & #ImageID# & "." & #Ext#>

<cffile action = "rename"
source = "#Src#"
destination = "#Dest#"
attributes="normal">

<cfset PixID = PixID + 1>
#ImageName# has been uploaded. <br>

<cfelse>
Image #i# has NOT been uploaded due to missing image location<br>
</cfif>
</cfloop>
</cfoutput>
 
Hi dmxd99,

It would appear that you just need to change your code from
Code:
<cffile action="upload"
                filefield="#variables.FileName#"    
                destination="#Path#"
                ACCEPT="image/jpg, image/pjpeg,image/gif, application/msword, image/bmp, image/tiff"
                nameconflict="OVERWRITE">

to
Code:
<cffile action="upload"
                filefield="#variables.FileName#"    
                destination="#Path#"
                ACCEPT="image/jpg, image/pjpeg,image/gif, application/msword, image/bmp, image/tiff"
                nameconflict="MakeUnique">


And besides that it would be advisable to change
Code:
<cffile action = "rename"    
                source = "#Src#"
                destination = "#Dest#"
                attributes="normal">
to
Code:
<!--- define the name of a backup file --->
<cfset BackupFile = ListFirst(dest, ".") & "_" & DateFormat(Now(), "dd_mmm_yyyy") & "_" & TimeFormat(now(), "HHmm_tt") & "." & ListLast(dest, ".")>

<!--- save a copy of a pre-existing file --->
<cfif FileExists(Dest)>
	<cffile   action="rename" 
                  source="#Dest#" 
                  destination="#BackupFile#" 
                  attributes="normal">  
</cfif>

<!--- rename our new file --->
<cffile  action="rename" 
         source="#Src#" 
         destination="#Dest#" 
         attributes="normal">

as this would allow you to save a backup copy of an existing file with your destination name.



other then that it looks kind of alright to me.

cheers,
B.

We never fail, we just find that the path to succes is never quite what we thought...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top