Link to home
Start Free TrialLog in
Avatar of Coast Line
Coast LineFlag for Canada

asked on

trouble with renaming the uploaded files

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
Avatar of Sudhindra A N
Sudhindra A N
Flag of India image

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">    
ASKER CERTIFIED SOLUTION
Avatar of duncancumming
duncancumming
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of gdemaria
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.
Avatar of Coast Line

ASKER

Thanks i found another way also, but urs is also cool