Skip to main content
February 2, 2011
Answered

Question about cffile

  • February 2, 2011
  • 3 replies
  • 1961 views

Him I am using the following cffile to upload an image to a folder on the website, but wanted to know if there a way that it will rename the file according to the userID in the database. For example.... image name is jackson.jpg and Jackson's userID is 12, so i would like the image to be renamed to 12.jpg. Hope thats makes sense. I know this is a super simple upload system which is why i need help. Thanks

======= cffile code =============

<cfif isDefined("fileUpload")>
  <cffile action="upload"
           fileField="fileUpload"
           destination="C:\ColdFusion9\wwwroot\myWebsite\user_images"
          accept="image/jpg,image/jpeg">
     <p>Thank you, your file has been uploaded.</p>
</cfif>
<cfform enctype="multipart/form-data" method="post">
  <cfinput type="file" name="fileUpload" />
  <cfinput type="submit" name="file_submit" value="Upload File" />
</cfform>

    This topic has been closed for replies.
    Correct answer -__cfSearching__-

    Can't figure out why it works in FF, but not IE.

    | h1. The MIME type of the uploaded file image/pjpeg

    Only files of type image/JPG,image/JPEG can be uploaded.

    The reason is right there in the error message ;-) "image/Pjpeg" is not in the list of mime types you are accepting. Internet Explorer often has its own ideas about mime types.

    That said, do not rely on the "accept" attribute. Mime types are easy to exploit:

    Message was edited by: -==cfSearching==-

    3 replies

    BKBK
    Community Expert
    Community Expert
    February 4, 2011

    accept="image/*"

    can be quite handy as default for image files.

    Inspiring
    February 4, 2011

    Yes, but not very safe.

    Participant
    February 4, 2011

    Korrupt, try this:

    <cfif isDefined("fileUpload")>
      <cffile action="upload"

              fileField="fileUpload"
              destination="C:\ColdFusion9\wwwroot\myWebsite\user_images"
              accept="image/jpg,image/jpeg" />

      <cffile action="rename"

              source=cffile.serverDirectory & cffile.serverFile

              destination=cffile.serverDirectory & {newFileName} & cffile.serverFileExt />


      <p>Thank you, your file has been uploaded.</p>
    </cfif>


    <cfform enctype="multipart/form-data" method="post">
      <cfinput type="file" name="fileUpload" />
      <cfinput type="submit" name="file_submit" value="Upload File" />
    </cfform>

    Replace {newFileName} with the variable holding the string of whatever you want the file name to be.

    Just a heads up about about the cffile scope: when you use the cffile tag to upload a file to the server, the cffile scope is populated with data about the upload attempt.  You can also specify a variable name to be filled with the data in the result attribute if you prefer. See the LiveDocs page on cffile action="upload".

    Message was edited by: G. Taylor

    February 4, 2011

    Thanks for the posts. I got it to work in FF just fine and the way i want it to, but in IE i get this error and i don't know why.

    Can't figure out why it works in FF, but not IE.

    ---------- Error message -----------

    The MIME type of the uploaded file image/pjpeg was not accepted by the server.


    Only files of type image/jpg,image/jpeg can be uploaded.
    Verify that you are uploading a file  of the appropriate type.
    The  error occurred in  C:\ColdFusion9\wwwroot\poll_system\admin_section\picture_upload.cfm: line 23
    21 :             fileField="fileUpload"
    22 :             destination="C:\ColdFusion9\wwwroot\Client\Client_images"
    23 :           accept="image/jpg,image/jpeg">
    24 :     <font size="+1">The file has been uploaded.</font><br />
    25 :     Don't forget to add the image name to the "Politician Image" field.<br />
    -__cfSearching__-Correct answer
    Inspiring
    February 4, 2011

    Can't figure out why it works in FF, but not IE.

    | h1. The MIME type of the uploaded file image/pjpeg

    Only files of type image/JPG,image/JPEG can be uploaded.

    The reason is right there in the error message ;-) "image/Pjpeg" is not in the list of mime types you are accepting. Internet Explorer often has its own ideas about mime types.

    That said, do not rely on the "accept" attribute. Mime types are easy to exploit:

    Message was edited by: -==cfSearching==-

    Inspiring
    February 2, 2011

    After you upload thefile, you can rename it by using this in Coldfusion using the action = rename of CFFILE..

    or

    I hope you can do that in Javascript also, while uploading you will be calling some function
    to check whether its an acceptable format, in that function you could call the rename function.

    cffile action = "rename"

    <!--- Source Document is read-only but when renamed it becomes normal (not hidden or  
        read-only). --->
    <cffile action = "rename" source = "c:\files\memo\readonlymemo.doc" 
        destination = "c:\files\memo\normalmemo.doc" attributes="normal">





    February 3, 2011

    Thanks for your post. How would i link the two?

    Inspiring
    February 3, 2011

    No linking..

    Its the two options you could pick anyone which is simple.

    Try it and let me know.