Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 522
  • Last Modified:

UPLOADING / RENAMING A FILE

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"
                 filefield="CVFile"
                 destination="D:\WWWRoot\www\uploads\"
                 nameconflict="Makeunique">
        <!--- 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>">
            <cfelse>
            <!--- 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#
            </cfquery>
            </cfif>
            </cftransaction>
      <cfelse>
            <cfset errormessage = errormessage &
            "<li style=color:red><font class=mainBlack>File too Large. Cannot be greater than 200kb.</font></li>">
      </cfif>
      <cfelseif form.CVFile eq "" and #UserDetails.CVLink# eq "">
       <cfset errormessage = errormessage &
            "<li style=color:red><font class=mainBlack>Please attach your CV.</font></li>">
      </cfif>

Random ID Code (Random_CVIDGen)

<html>
<head>
<title>Random ID</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<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">

<body>

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

<!--- 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, "|")>
        </cfif>
    </cfcase>
    <!--- Create Alphanumeric passwords --->
    <cfcase value="alphanumeric">
        <cfset charlist = "#alphaLcase#|#numeric#">
        <cfif ATTRIBUTES.UCase IS "Yes">
            <cfset charList = listappend(charlist, alphaUcase, "|")>
        </cfif>
    </cfcase>
    <!--- Create only numeric passwords --->
    <cfcase value="numeric">
        <cfset charlist = numeric>
    </cfcase>

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

<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, " ")>

</cfloop>

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

</body>
</html>

Thanks for your help guys !!
0
jturkington
Asked:
jturkington
4 Solutions
 
mrichmonCommented:
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.
0
 
mrichmonCommented:
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.
0
 
Tacobell777Commented:
******* here you save the file with a unique name *******
        <cffile action="upload"
                 filefield="CVFile"
                 destination="D:\WWWRoot\www\uploads\"
                 nameconflict="Makeunique">

******* 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.
0
Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

 
LeaperJPDCommented:

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#">
</cfif>

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.

cheers,

Jason
0
 
anandkpCommented:
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 ISDEFINED('NEW_FIELDNAME') AND TRIM(NEW_FIELDNAME) NEQ '' AND CGI.CONTENT_LENGTH LT 25000><!--- Upload --->
      <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#
            </CFQUERY>
            <CFIF DEL_IMG.RECORDCOUNT GT 0 AND FILEEXISTS('#FILEPATH##DEL_IMG.NEW_FIELDNAME#')>
                  <CFFILE ACTION="DELETE" FILE="#FilePath##Del_Img.New_FieldName#">
            </CFIF>
            <CFSET NEW_IMAGENAME = ""><!--- Since image is deleted - set it to blank --->
      </CFIF>
      <CFTRY>
          <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 FILEEXT = LISTLAST(FILE.SERVERFILE,'.')><!--- RENAME HERE --->
        <CFFILE ACTION="RENAME" SOURCE="#FilePath##FILE.SERVERFILE#" DESTINATION="#FilePath##NewFileName#.#FILEEXT#">
        <CFSET NEW_IMAGENAME = "#NewFileName#.#FILEEXT#">                      
             
          <CFCATCH>
            <SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
            <!--
            alert('image not selected properly pls select the image again')
            history.back(-1)
            //-->
            </SCRIPT>        
        </CFCATCH>
    </CFTRY>
      <CFQUERY NAME="Upd_Table" DATASOURCE="#DSN#">
            Update Table set ImageName = '#New_ImageName#'
      </CFQUERY>
<CFELSE>
    <!--- Keep the Image As It Is ! --->
    <!--- Dont do ne-thing here --->
</CFIF>
0
 
mrichmonCommented:
Did any of these comments help you or are you still stuck?
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Tackle projects and never again get stuck behind a technical roadblock.
Join Now