Solved

UPLOADING / RENAMING A FILE

Posted on 2004-03-31
6
506 Views
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"
                 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
Comment
Question by:jturkington
6 Comments
 
LVL 35

Assisted Solution

by:mrichmon
mrichmon earned 100 total points
Comment Utility
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
 
LVL 35

Expert Comment

by:mrichmon
Comment Utility
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
 
LVL 17

Assisted Solution

by:Tacobell777
Tacobell777 earned 100 total points
Comment Utility
******* 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 2

Assisted Solution

by:LeaperJPD
LeaperJPD earned 100 total points
Comment Utility

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
 
LVL 17

Accepted Solution

by:
anandkp earned 200 total points
Comment Utility
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
 
LVL 35

Expert Comment

by:mrichmon
Comment Utility
Did any of these comments help you or are you still stuck?
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Most ColdFusion developers get confused between the CFSet, Duplicate, and Structcopy methods of copying a Structure, especially which one to use when. This Article will explain the differences in the approaches with examples; therefore, after readin…
Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now