Hi
I got the below code from the internet, Im now trying to add the users profile name and date and time to the start of the file name which the user uploads
anyone any tips on how this might be done? or if it can be done at all
<cfif isDefined("fileUpload")>
<cffile action="upload"
fileField="fileUpload"
destination="\\\\TEST\\TEST\\TEST\\TEST\\Documents">
<p>Thankyou, your file has been uploaded.</p>
</cfif>
<form enctype="multipart/form-data" method="post">
<input type="file" name="fileUpload" /><br />
<input type="submit" value="Upload File" />
</form>
took the below from something that I have in production. Should be generic enough to work for you. Put this in a cfc and you’re good to go. There’s no error checking on a failed upload, you’ll need to add that yourself.
<cffunction name="upload_file" returnType="boolean" access="public" description="uploads a file to the system">
<cfargument name="filefield" type="string" required="true" default="#ARGUMENTS[1]#" />
<cfargument name="path" type="string" required="true" default="#ARGUMENTS[2]#">
<!--- upload file to server --->
<cffile action="upload" fileField="#ARGUMENTS.filefield#" destination="#ExpandPath('#ARGUMENTS.path#')#" mode="777">
<!--- do whatever you need to do to get user profile info here --->
<cfquery name="user_info" datasource="#APPLICATION.dsn#">
PROBABLY A QUERY
</cfquery>
<!--- rename file --->
<cfset new_filename = "#user_info.profile_name &"-"& DateFormat(Now(),'mmddyy') &"-"& TimeFormat(Now(),'HHmm') &"."& cffile.clientFileExt#">
<cffile action="rename" source="#ExpandPath('#ARGUMENTS.path#/#cffile.serverFile#')#" destination="#ExpandPath('#ARGUMENTS.path#/#new_filename#')#" mode="777">
<cfreturn true>
</cffunction>
Thanks for that downtroden
I now need to save the file name and location in a table and creat a hyperlink so I can pick it up on a different form
can this be done easily?
do a sql insert after using the cffile tag - you can get the filename by using file.serverfile (make sure you put the location of the file first. example:
Hi cbrickhouse
Ive tried your above code but it keeps giving me the below error:
Invalid content type: ‘’.
The cffile action=“upload” requires forms to use enctype=“multipart/form-data”.
i do try to ‘var’ when I can, but what are you supposed to do when the value of your variable is dependent on something happening before it? The CFML engine starts squawking about how var’s need to be at the beginning of the function (even before arguments). So in this instance, you’d have to name the var but the neither the upload or the user_info query have fired, which the new_filename variable is EXTREMELY dependent on.
Var scoped variables should not come before cfargument tags. That’s not a requirement.
Here’s what we do in our production environment. We create a explicitly structure called LOCAL, then all possible variables get created into that struct. Cffile is different because it itself returns a query object, but cannot be var into another scope.
<cffunction name="upload_file" returnType="boolean" access="public" description="uploads a file to the system">
<cfargument name="filefield" type="string" required="true" default="#ARGUMENTS[1]#" />
<cfargument name="path" type="string" required="true" default="#ARGUMENTS[2]#">
<cfset var LOCAL = {}>
<cfset var cffile = ''>
<!--- upload file to server --->
<cffile action="upload" fileField="#ARGUMENTS.filefield#" destination="#ExpandPath('#ARGUMENTS.path#')#" mode="777">
<!--- do whatever you need to do to get user profile info here --->
<cfquery name="LOCAL.user_info" datasource="#APPLICATION.dsn#">
PROBABLY A QUERY
</cfquery>
<!--- rename file --->
<cfset LOCAL.new_filename = "#user_info.profile_name &"-"& DateFormat(Now(),'mmddyy') &"-"& TimeFormat(Now(),'HHmm') &"."& cffile.clientFileExt#">
<cffile action="rename" source="#ExpandPath('#ARGUMENTS.path#/#cffile.serverFile#')#" destination="#ExpandPath('#ARGUMENTS.path#/#new_filename#')#" mode="777">
<cfreturn true>
</cffunction>
Also a note that in ColdFusion 9 var scoped variables can be created anywhere inside the function, not just at the top. Also to note that in ColdFusion 9, CF will implicitly scope all variables into a “local” scope, in much the same way my code does.