Solved

trouble with renaming the uploaded files

Posted on 2010-09-12
5
340 Views
Last Modified: 2012-05-10
i have the following code which works fine but my isse is that how can i rename the file to a specific format

here is my code!

<cftry>
<cfset path = "#filepath#\images\inventory\">
<cfset filter = "image/jpg,image/jpeg,image/pjpeg">
<cfif form.userfile IS NOT "">
  <cffile action="upload" fileField="userfile" destination="#path#" result="fileUpload" nameconflict="overwrite" accept="#filter#">
      <cfif fileUpload.fileWasSaved>
    <!--- Determine whether the saved file is a valid image file. --->
    <cfif IsImageFile("#fileUpload.serverDirectory#/#fileUpload.serverfile#")>
      <!--- Read the image file into a variable called myImage. --->
      <cfimage action="read" source="#fileUpload.serverDirectory#/#fileUpload.serverfile#" name="myImage">
                  <cfset ImageResize(myImage,#form.width#,#form.height#,"HIGHESTQUALITY",1)>
              <cfset ImageResize(myImage,#form.fullwidth#,#form.fullheight#,"HIGHESTQUALITY",1)>
                   <cfset form.newImageName = fileUpload.serverFilename & "_front1_thumb." & fileUpload.serverFileExt>
            <cfimage source="#myImage#" action="write" destination="#path#/#form.newImageName#" overwrite="yes">    
     
      <cfelse>
          <cffile action="delete" file="#fileUpload.serverDirectory#/#fileUpload.serverFile#">
    </cfif>        
</cfif>

i want to convert the upload file as this format!

#numberformat(itemid,'0000')#_front1_thumb.jpg

I am really lost here! please guide
0
Comment
5 Comments
 
LVL 13

Expert Comment

by:ansudhindra
ID: 33660066
Could you please try the below code?

<cfset newImageName = "#fileUpload.serverFilename#_front1_thumb.#fileUpload.serverFileExt#">
            <cfimage source="#myImage#" action="write" destination="#path#/#newImageName#" overwrite="yes">    
0
 
LVL 16

Accepted Solution

by:
duncancumming earned 500 total points
ID: 33665807
So right now you're doing:
<cfset form.newImageName = fileUpload.serverFilename & "_front1_thumb." & fileUpload.serverFileExt>
<cfimage source="#myImage#" action="write" destination="#path#/#form.newImageName#" overwrite="yes">    

But you want to do:
#numberformat(itemid,'0000')#_front1_thumb.jpg

Where does itemid come from?  Also it seems you'll be completely ditching any reference to the uploaded file name?  In which case why not just do:

<cfset newImageName = "#numberformat(itemid,'0000')#_front1_thumb.jpg">

<cfimage source="#myImage#" action="write" destination="#path#/#newImageName#" overwrite="yes">    
0
 
LVL 4

Expert Comment

by:sandy12879
ID: 33669541
You need to have a unique ID associated while renaming your Images  and u can use the same piece of code mentioned by ansudhindra
<cfset newImageName = "#fileUpload.serverFilename#_front1_thumb.#fileUpload.serverFileExt#">
            <cfimage source="#myImage#" action="write" destination="#path#/UNIQUEID#newImageName#" overwrite="yes">
This will ensure uniqueness for the file's having similar name.
0
 
LVL 39

Expert Comment

by:gdemaria
ID: 33681992
Suggestion, I recommend renaming the file on the server completely remove the client's file name (as Duncan suggested).

There are several characters that can be in a file name that will not be allowed on the server or will cause issues when handling through coldfusion.  I always rename the file to just the ID associated with the database record   #file_id#.#fileUpload.serverFileExt#     That makes handling the file much easier.

Simply save the client file name in your database, when downloading the file back to the user (if you ever need to) you can use their original file name again so they never need to know the name of the file on your server.
0
 
LVL 15

Author Closing Comment

by:Gurpreet Singh Randhawa
ID: 33710766
Thanks i found another way also, but urs is also cool
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What is the normal amount of queries 5 74
Cold Fusion Session Timing Out 11 59
Problem to Popup 2 74
Can I evaluate a variable in the value attribute of <cfhttpparam>? 2 28
Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

932 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

10 Experts available now in Live!

Get 1:1 Help Now