Posted on 2004-03-31
Last Modified: 2013-12-24
We are having an issue when we upload a file to our website in that it is not renaming the file correctly and is crashing out.
The original file is successfully uploading with its original name but is not renaming to the value we want. Is this because the file is not fully uploaded before trying to rename it ? or is it our renaming convention that is causing the issue do we need to use UUID function maybe ? The security on the folder it is uploading to has more than enough permissions, heres the code:  -

<cfif IsDefined("Process")>
<CF_Random_CVIDGen LENGTH="10" CHARSET="AlphaNumeric" UCASE="yes" RETURNVARIABLE="CVUniqueID">

<cfif form.CVFile neq "" and #UserDetails.CVLink# eq "">
<cfif cgi.CONTENT_LENGTH lt 200000>
<!--- first actually upload the file --->
         <cffile action="upload"
        <!--- now create a temporary holder for the attachment later on --->
        <cfset Filename = "#cffile.clientfilename#">
            <cfset acceptedExtensions="doc,txt,text,rtf,pdf">
            <cfset ext=ListLast(cffile.ServerFile,".")>
            <!--- check the file types of the file --->
            <cfif not ListFind(acceptedExtensions,ext)>
<cffile action="delete" file="D:\WWWRoot\www\uploads\#file.ServerFile#">
                  <cfset errormessage = errormessage &
                  "<li style=color:red><font class=mainBlack>Please attach a valid CV.</font></li>">
            <!--- its a good file --->
            <cffile action = "rename"
                     source = "D:\WWWRoot\www\uploads\#file.ServerFile#"
                     destination = "D:\WWWRoot\uploads\#UserDetails.Email##CVUniqueID#.#serverFileExt#">
            <cfset FileUploaded = "D:\WWWRoot\uploads\#UserDetails.Email##CVUniqueID#.#serverFileExt#">

            <cfquery name="UpdateCandidates" datasource="Datasource">
            update Candidates
            set CVLink = '#FileUploaded#',
            Filename = '#Filename#',
            CVUpdated = '#DateFormat(Now(),"dd/mm/yy")# #TimeFormat(Now(),"HH:MM:SS")#'
            where CandID = #SESSION.Auth.CandID#
            <cfset errormessage = errormessage &
            "<li style=color:red><font class=mainBlack>File too Large. Cannot be greater than 200kb.</font></li>">
      <cfelseif form.CVFile eq "" and #UserDetails.CVLink# eq "">
       <cfset errormessage = errormessage &
            "<li style=color:red><font class=mainBlack>Please attach your CV.</font></li>">

Random ID Code (Random_CVIDGen)

<title>Random ID</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<cfparam name="ATTRIBUTES.Length" default="6">

<!--- types of characters to use in the password (default Uppercase, Lowercase letters and 0-9 --->
<cfparam name="ATTRIBUTES.CharSet" default="alpanumeric">

<!--- name of the variable to store the generated password (default RandomPassword) --->
<cfparam name="ATTRIBUTES.ReturnVariable" default="RandomPassword">

<!--- yes/no include uppercase characters in the password? (default yes)--->
<cfparam name="ATTRIBUTES.Ucase" default="yes">


<cfif NOT isnumeric(ATTRIBUTES.Length)>
    <cfthrow detail="The attribute <b>Length</b> must be a valid numeric value">

<!--- valid CharSets are handled as the defaultcase below --->

<!--- ATTRIBUTES.Ucase is only checked for as "YES" if it is NOT "YES" uppercase letters are simply not included
no errors will be thrown no matter what you specify as this attribute--->

<cfset alphaLcase  = "a|c|e|g|i|k|m|o|q|s|u|w|y|b|d|f|h|j|l|n|p|r|t|v|x|z">
<cfset alphaUcase = "A|C|E|G|I|K|M|O|Q|S|U|W|Y|B|D|F|H|J|L|N|P|R|T|V|X|Z">
<cfset numeric      = "0|2|4|6|8|9|7|5|3|1">

<!--- decide what cars to use in generating the password --->
<cfswitch expression="#ATTRIBUTES.CharSet#">
    <!--- Create only alpha passwords (NO NUMBERS ALLOWED) --->
    <cfcase value="alpha">
        <cfset charlist = alphaLcase>
        <cfif ATTRIBUTES.UCase IS "Yes">
            <cfset charList = listappend(charlist, alphaUcase, "|")>
    <!--- Create Alphanumeric passwords --->
    <cfcase value="alphanumeric">
        <cfset charlist = "#alphaLcase#|#numeric#">
        <cfif ATTRIBUTES.UCase IS "Yes">
            <cfset charList = listappend(charlist, alphaUcase, "|")>
    <!--- Create only numeric passwords --->
    <cfcase value="numeric">
        <cfset charlist = numeric>

        <cfthrow detail="Valid values of the attribute <b>CharSet</b> are Alpha, AlphaNumeric, and Numeric">

<cfparam name="ThisPass" default="">

<!--- each loop count gets one new character and adds it to the end of the password,
so loop from 1 to "the number set in the calling template in ATTRIBUTES.Length --->
<cfloop from="1" to="#ATTRIBUTES.Length#" index="i">

    <!--- pick a random number between 1 and the length of the list of chars to use --->
    <cfset ThisNum = RandRange(1,listlen(charlist, "|"))>

    <!--- get the character that is at the random numbers position in the list of characters --->
    <cfset ThisChar = ListGetAt(Charlist, ThisNum, "|")>

    <!--- append the new random character to the password --->
    <cfset ThisPass = ListAppend(ThisPass, ThisChar, " ")>


<cfset ThisPass = replace(ThisPass, " ", "", "ALL")>
<cfset "Caller.#ATTRIBUTES.ReturnVariable#" = ThisPass>


Thanks for your help guys !!
Question by:jturkington
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 35

Assisted Solution

mrichmon earned 100 total points
ID: 10727509
It is highly likely that it is your naming process.

Have your tried hard coding in a name of the file and running an upload.  If this works then it is definatley your naming convention.
LVL 35

Expert Comment

ID: 10727515
You could also try doing a MOVE instead of a rename - which would in effect do the same thing, but may work when renaming doesn't.
LVL 17

Assisted Solution

Tacobell777 earned 100 total points
ID: 10727736
******* here you save the file with a unique name *******
        <cffile action="upload"

******* Name of the file uploaded from the client's system, so you are setting the variable filename to the original filename not the uniquely renamed file, I think you are looking for cffile.serverFile here *******

        <!--- now create a temporary holder for the attachment later on --->
        <cfset Filename = "#cffile.clientfilename#">

******* Or should file.serverfile not be cffile.serverfile *******

<!--- its a good file --->
          <cffile action = "rename"
                  source = "D:\WWWRoot\www\uploads\#file.ServerFile#"
                  destination = "D:\WWWRoot\uploads\#UserDetails.Email##CVUniqueID#.#serverFileExt#">
Thats just a quick scan through the code, I could be wrong.
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)


Assisted Solution

LeaperJPD earned 100 total points
ID: 10736695

You asked:  "Is this because the file is not fully uploaded before trying to rename it ?"
- no, the file must be uploaded before it get's it's name returned by coldfusion (#ServerFileName#.#ServerFileExt#)

Once it is uploaded, check for it's existence, then rename it:

<!--- if the file exists, rename the file --->
<cfif fileexists("#directorypath#\#ServerFileName#.#ServerFileExt#")>
<cffile action="rename" source="#directorypath#\#ServerFileName#.#ServerFileExt#" destination="#directorypath#\somenewfilename.#ServerFileExt#">

Of course if the file does not exist then there are problems to be dealt with first. I know you said you have permissions enabled, but if you do not have the right, this stuff won't work, and you may not receive any error messages as to why it's not working.  Permissions problems sometimes tend to blackhole problems, leaving you with few clues as to what happened.


LVL 17

Accepted Solution

anandkp earned 200 total points
ID: 10755834
this is what ive used ...

1. New_FieldName = name of ur file_field on the previous page inside the form tag
2. Image_FieldName = name of the exisiting image
    [used only for deleting previous rec / id can also be used. alter the query below as required]
3. FilePath = full physical path where file needs to be uplaoded OR is uploaded
4. New_ImageName = variable used for assignign new name to the file after uplaod
    [incase of rename option being used]
5. FILEEXT = the extension of the file uploaded - so renaming can be handled properly
6. NewFileName = incase u wanna rename the file- the file uplaoded gets renamed with this value.

      <CFIF ACT EQ "EDIT"><!--- Del existing one - only incase of edit mode !!! --->
            <CFQUERY NAME="Del_Img" DATASOURCE="#DSN#">
                  Select New_FieldName from TableName where New_FieldName = #Image_FieldName#
                  <CFFILE ACTION="DELETE" FILE="#FilePath##Del_Img.New_FieldName#">
            <CFSET NEW_IMAGENAME = ""><!--- Since image is deleted - set it to blank --->
          <CFFILE ACTION="UPLOAD" FILEFIELD="New_FieldName" DESTINATION="#FilePath#" NAMECONFLICT="MakeUnique" ACCEPT="image/jpg">
            <CFSET NewFileName = "anand"><!--- renaming the uploaded file to anand - change this to whatever in ur code --->
        <CFSET NEW_IMAGENAME = "#NewFileName#.#FILEEXT#">                      
            <SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
            alert('image not selected properly pls select the image again')
            Update Table set ImageName = '#New_ImageName#'
    <!--- Keep the Image As It Is ! --->
    <!--- Dont do ne-thing here --->
LVL 35

Expert Comment

ID: 10809312
Did any of these comments help you or are you still stuck?

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In our day to day coding, how many times have we come across a necessity to check whether a URL is a broken link or not? For those of you that answered countless and are using ColdFusion like myself, then this article is for you.  It will show yo…
Periodically we have to update or add SSL certificates for customers. Depending upon your hosting plan you may be responsible for the installation and/or key generation. In the wake of Heartbleed many sites were forced to re-key. We will concen…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question