Solved

trouble with renaming the uploaded files

Posted on 2010-09-12
5
344 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
[X]
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
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 16

Author Closing Comment

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

Featured Post

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

CFGRID Custom Functionality Series -  Part 1 Hi Guys, I was once asked how it is possible to to add a hyperlink in the cfgrid and open the window to show the data. Now this is quite simple, I have to use the EXT JS library for this and I achiev…
I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authori…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

717 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