Solved

UPLOADING / RENAMING A FILE

Posted on 2004-03-31
6
511 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
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.
0
 
LVL 35

Expert Comment

by:mrichmon
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.
0
 
LVL 17

Assisted Solution

by:Tacobell777
Tacobell777 earned 100 total points
ID: 10727736
******* 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 2

Assisted Solution

by:LeaperJPD
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#">
</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
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 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
ID: 10809312
Did any of these comments help you or are you still stuck?
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to enable php7 module in nginx configuration file 8 411
UPLOAD FILE TO Web API USING POST 5 117
Connect to MS-SQL server from Linux/PHP 8 85
cookies analysis tools 2 93
A web service (http://en.wikipedia.org/wiki/Web_service) is a software related technology that facilitates machine-to-machine interaction over a network. This article helps beginners in creating and consuming a web service using the ColdFusion Ma…
When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

822 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