Solved

trouble with renaming the uploaded files

Posted on 2010-09-12
5
339 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
Question by:myselfrandhawa
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:myselfrandhawa
ID: 33710766
Thanks i found another way also, but urs is also cool
0

Featured Post

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

Join & Write a Comment

Suggested Solutions

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…
Sometimes databases have MILLIONS of records and we need a way to quickly query that table to return the results me need. Sure you could use CFQUERY but it takes too long when there are millions of records. That is why SOLR was invented. Please …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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)

706 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

15 Experts available now in Live!

Get 1:1 Help Now