Skip to main content
Participant
October 7, 2011
Question

uploading a file, renaming it and placing it in a particular location on server

  • October 7, 2011
  • 1 reply
  • 1347 views

A user submits a file from a front end HTML form which has fields like Division, Department name, department number,

section number, year, email, phone etc. The file being submitted might have a user given name. But, when it is 

uploaded I want it to be named as Departmentname_departmentnumber_sectionnumber. So, if department is Accounting, 

dept number is 123 and section is 1, name of file will be Accounting_123_1.doc The extension will be whatever type 

of file(text, MS-Word's .doc or .docx, PDF or RTF) was submitted and the user can upload attachments of files with 

extension .txt, .doc, .docx, pdf, rtf only.

Also, I want it to be stored on a particular location on server. So, if Division is Corporate Finance and year is 

2011-2012 it should be stored on server at "E:\Files Submitted\2011-2012\Corporate Finance\". The "E:\Files

Submitted\" part remains same in the directory name.

<cfset submittedfileName =

#form.departmentname#&"_"&#form.departmentnumber#&"_"&#form.section_number_1#&"."&#cffile.ClientFileExt#>

<cfset filedirectoryYear = "E:\Files Submitted\"&#form.current_year#&"\"&#form.division#&"\">

<!--- ensure that the user uploads attachments of type with extension .txt, .doc, .docx, pdf, rtf only--->

<cfif FORM.attachment_1 neq "">

   

      

       <cffile action="upload"

               accept="text/plain,application/msword,application/pdf,application/rtf"          

                 filefield="attachment_1"

                 destination="E:\temp\uploads"

                 nameconflict="Makeunique"

        >

<!--- rename the file and move it to permanent destination --->        

       

        <cffile

  action="rename"

  source="E:\temp\uploads\#cffile.serverFileName#"

  destination=#filedirectoryYear#&#submittedfileName#&#cffile.ClientFileExt#

  >

<!---   now create a temporary variable for the attachment so that it can be emailed later on --->

<cfset attachment_local_file_1 = #filedirectoryYear#&#submittedfileName#&#cffile.ClientFileExt#>

       

</cfif>

I used the cffile.ClientFileExt because the files were getting uploaded without the extension but am receiving an

error at   destination=#filedirectoryYear#&#submittedfileName#&#cffile.ClientFileExt# as "multiple items at this

position: Missing Token > or /> .

I am using Coldfusion 8. Any suggestions would be appreciated on where I am erring and how I can fix it.

This topic has been closed for replies.

1 reply

Inspiring
October 7, 2011

In this part,

destination=#filedirectoryYear#&#submittedfileName#&#cffile.ClientFil eExt#

the space between the l and e looks problematic.

m_j_95Author
Participant
October 7, 2011

Thanks Dan,

The space between 'l' and 'e' was a formatting error while I was pasting text to this forum.

Currently, I have

<cfset filedirectoryYear = "E:\Files Submitted\"&#form.current_year#&"\"&#form.division#&"\">

<cfif FORM.attachment_1 neq "">

       <cffile action="upload"

               accept="text/plain,application/msword,application/pdf,application/rtf "          

                 filefield="attachment_1"

                 destination="E:\temp\uploads"

                 nameconflict="Makeunique"

        >

<!--- rename the file and move it to permanent destination --->        

<cfset submittedfileName =

#form.departmentname#&"_"&#form.departmentnumber#&"_"&#form.section_number_1#&"."&#cffile.ClientFileExt#>

<cfset presentfileName = #cffile.serverFileName#&"."&#cffile.ClientFileExt#>

        <cffile

  action="rename"

  source="E:\temp\uploads\#presentfileName#"

  destination=#filedirectoryYear##submittedfileName#

  >

<!---   now create a temporary variable for the attachment so that it can be emailed later on --->

<cfset attachment_local_file_1 = #filedirectoryYear#&#submittedfileName#&#cffile.ClientFileExt#>

</cfif>

<cfset attachment_local_file_1 = #filedirectoryYear#&#submittedfileName#>

When I submit, I get the error message

"Attribute validation error for tag CFFILE.

The value of the attribute source, which is currently c:\Course Syllabi\uploads\Web Based System Two Page HandOut.pdf, is invalid and the line below is referred.

destination="#filedirectoryYear##submittedfileName#"

"

The filedirectoryYear path exists i.e. I have E:\Files Submitted\2011-2012\Finance. What could be the issue?

P.S. I got it working, The filedirectoryYear path value was not the actual directory path\name in the filesystem. Once I had the

correct name in the filesystem, the application worked fine.