Skip to main content
Inspiring
October 1, 2006
Answered

Get filename after upload - cffile

  • October 1, 2006
  • 7 replies
  • 4217 views
According to Livedocs when you use <cffile> the name of the uploaded file can be referenced. I put <cfoutput>#cffile.serverFile#</cfoutput> on the result page after the upload but and get the error: "serverfile is undefined in cffile".

Obviously it's not as simple as I thought. Did I miss something in my reading?



    This topic has been closed for replies.
    Correct answer ghouser
    SOLUTION: The following code uploads an image and places into the database - event tname, image name, image height, image height, and text for a caption to go with the image.

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

    <html>
    <head>
    <title>Upload Event Photos</title>
    <link rel=stylesheet type="text/css" href="style.css">
    <cfscript>
    /**
    * Returns width and height of images based on image type.
    *
    * @param filename Absolute or relative path to file. (Required)
    * @param mimetype Minetype for the file. (Optional)
    * @return Returns a struct containing height and width information, or an error string.
    * @author Peter Crowley (&#112;&#99;&#114;&#111;&#119;&#108;&#101;&#121;&#64;&#119;&#101;&#98;&#122;&#111;&#110;&#101;&#46;&#105;&#101;)
    * @version 1, August 17, 2006
    */
    function ImageSize(filename) {
    // Jpeg variables
    var nFileLength=0; var nBlockLength=0; var nMarker=0;
    var nSOI = 65496; // Start of Image (FFD8)
    var nEOI = 65497; // End of Image (FFD9)
    var nSOF = 65472; // Start of frame nMarker (FFC0)
    var nSOF1 = 65473; // Start of frame extended sequential mode (FFC1)
    var nSOF2 = 65474; // Start of frame progressive mode (FFC2)
    var nSOF3 = 65475; // Start of frame lossless mode (FFC3)
    var nSOS = 65498; // Start of Scan (FFDA)
    var sImageType = "";
    var kCoords = structNew();
    var fInput = 0;
    var sByte=0;
    var sFullPath="";
    var sMimeType = "";

    if (Left(filename,1) IS "/" OR Left(filename,1) IS "\" OR MID(filename,2,1) IS ":")
    sFullPath=filename;
    else
    sFullPath=ExpandPath(filename);

    // Establish image type
    if(arrayLen(arguments) gt 1) { //optional mimetype
    sMimeType = arguments[2];
    if (LCase(ListFirst(sMimeType,"/")) IS NOT "image") return "Wrong mime type";
    if (ListLen(sMimeType,"/") NEQ 2) return "Invalid mime type";
    sImageType=LCase(ListLast(sMimeType,"/"));
    } else { // work off file extension
    if (ListLen(filename,".") LT 2) return "Unknown image type";
    sImageType=LCase(ListLast(filename,"."));
    }

    if(not fileExists(sFullPath)) return "File does not exist.";

    //make a fileInputStream object to read the file into
    fInput = createObject("java","java.io.RandomAccessFile").init(sFullPath,"r");

    // Get X,Y resolution sizes for each image type supported
    switch (sImageType) {
    case "jpg": case "jpeg":
    do {
    nMarker = fInput.readUnsignedShort();

    if (nMarker NEQ nSOI AND nMarker NEQ nEOI AND nMarker NEQ nSOS) {

    nBlockLength = fInput.readUnsignedShort();

    if (nMarker EQ nSOF OR nMarker EQ nSOF1 OR nMarker EQ nSOF2 OR nMarker EQ nSOF3) { // Start of frame
    fInput.readUnsignedByte(); // skip sample precision in bits
    kCoords.ImageHeight = fInput.readUnsignedShort();
    kCoords.ImageWidth = fInput.readUnsignedShort();
    fInput.close();
    return kCoords;
    } else {
    fInput.skipBytes(JavaCast("int",nBlockLength-2));
    }
    }
    } while (BitSHRN(nMarker,8) EQ 255 AND nMarker NEQ nEOI);
    break;
    case "gif":
    fInput.skipBytes(6);

    sByte = fInput.readUnsignedByte();
    kCoords.ImageHeight = fInput.readUnsignedByte() * 256 + sByte;

    sByte = fInput.readUnsignedByte();
    kCoords.ImageWidth = fInput.readUnsignedByte() * 256 + sByte;

    fInput.close();
    return kCoords;
    default:
    break;
    }
    //close out this entry
    fInput.close();
    return "Unhandled image type";
    }
    </cfscript>



    </head>
    <body bottommargin="0" leftmargin="0" rightmargin="0" topmargin="0" background="../img/background.jpg">

    <!-------- page data goes below here ------>
    <!--------------->
    <div align="center"><h1><font color="#FF0000">Database Maintenance</font></h1></div>
    <br><br><br>

    <br><br>

    <div align="center"><table border="2" width="100">
    <tr>
    <td>

    <div align="center">Click here to upload a new Event Photo:
    <cfif isdefined("form.upload_now")>
    <cffile action="upload" filefield="ul_path" destination= "C:\CFusionMX7\wwwroot\aasr\eventpics\" nameconflict="overwrite">
    <cfset newfile = #cffile.serverfile#>

    <CFSCRIPT>
    kImageSize = ImageSize("#newfile#");
    </CFSCRIPT>

    <cfquery name="insert_eventpics" datasource="gph">
    insert into eventpics
    (eventname, picpath, imageheight, imagewidth, pictext)
    values
    ('#eventname#', '#newfile#', '#kImageSize.ImageHeight#', '#kImageSize.ImageWidth#', '#pictext#')
    </cfquery>


    </cfif>
    <form action="" method="post" name="upload_form" enctype="multipart/form-data" id="upload_form">
    <input type="File" name="ul_path" size="40" id="ul_path">
    </td>
    </tr>

    <tr>
    <td>
    Event name: <input type="text" name="eventname" id="eventname" size="40">
    </td>
    </tr>
    <tr>
    <td>
    Picture text: <input type="text" name="pictext" id="pictext" size="40">
    </td>
    </tr>
    <tr>
    <td>
    <div align="center"><input type="submit" name="upload_now" value="Submit"></div>
    </td>
    </tr>
    </form>




    </table>

    </div>
    <!-------- page data goes above here ------>
    <!--------------->
    </body>
    </html>


    7 replies

    ghouserAuthorCorrect answer
    Inspiring
    October 3, 2006
    SOLUTION: The following code uploads an image and places into the database - event tname, image name, image height, image height, and text for a caption to go with the image.

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

    <html>
    <head>
    <title>Upload Event Photos</title>
    <link rel=stylesheet type="text/css" href="style.css">
    <cfscript>
    /**
    * Returns width and height of images based on image type.
    *
    * @param filename Absolute or relative path to file. (Required)
    * @param mimetype Minetype for the file. (Optional)
    * @return Returns a struct containing height and width information, or an error string.
    * @author Peter Crowley (&#112;&#99;&#114;&#111;&#119;&#108;&#101;&#121;&#64;&#119;&#101;&#98;&#122;&#111;&#110;&#101;&#46;&#105;&#101;)
    * @version 1, August 17, 2006
    */
    function ImageSize(filename) {
    // Jpeg variables
    var nFileLength=0; var nBlockLength=0; var nMarker=0;
    var nSOI = 65496; // Start of Image (FFD8)
    var nEOI = 65497; // End of Image (FFD9)
    var nSOF = 65472; // Start of frame nMarker (FFC0)
    var nSOF1 = 65473; // Start of frame extended sequential mode (FFC1)
    var nSOF2 = 65474; // Start of frame progressive mode (FFC2)
    var nSOF3 = 65475; // Start of frame lossless mode (FFC3)
    var nSOS = 65498; // Start of Scan (FFDA)
    var sImageType = "";
    var kCoords = structNew();
    var fInput = 0;
    var sByte=0;
    var sFullPath="";
    var sMimeType = "";

    if (Left(filename,1) IS "/" OR Left(filename,1) IS "\" OR MID(filename,2,1) IS ":")
    sFullPath=filename;
    else
    sFullPath=ExpandPath(filename);

    // Establish image type
    if(arrayLen(arguments) gt 1) { //optional mimetype
    sMimeType = arguments[2];
    if (LCase(ListFirst(sMimeType,"/")) IS NOT "image") return "Wrong mime type";
    if (ListLen(sMimeType,"/") NEQ 2) return "Invalid mime type";
    sImageType=LCase(ListLast(sMimeType,"/"));
    } else { // work off file extension
    if (ListLen(filename,".") LT 2) return "Unknown image type";
    sImageType=LCase(ListLast(filename,"."));
    }

    if(not fileExists(sFullPath)) return "File does not exist.";

    //make a fileInputStream object to read the file into
    fInput = createObject("java","java.io.RandomAccessFile").init(sFullPath,"r");

    // Get X,Y resolution sizes for each image type supported
    switch (sImageType) {
    case "jpg": case "jpeg":
    do {
    nMarker = fInput.readUnsignedShort();

    if (nMarker NEQ nSOI AND nMarker NEQ nEOI AND nMarker NEQ nSOS) {

    nBlockLength = fInput.readUnsignedShort();

    if (nMarker EQ nSOF OR nMarker EQ nSOF1 OR nMarker EQ nSOF2 OR nMarker EQ nSOF3) { // Start of frame
    fInput.readUnsignedByte(); // skip sample precision in bits
    kCoords.ImageHeight = fInput.readUnsignedShort();
    kCoords.ImageWidth = fInput.readUnsignedShort();
    fInput.close();
    return kCoords;
    } else {
    fInput.skipBytes(JavaCast("int",nBlockLength-2));
    }
    }
    } while (BitSHRN(nMarker,8) EQ 255 AND nMarker NEQ nEOI);
    break;
    case "gif":
    fInput.skipBytes(6);

    sByte = fInput.readUnsignedByte();
    kCoords.ImageHeight = fInput.readUnsignedByte() * 256 + sByte;

    sByte = fInput.readUnsignedByte();
    kCoords.ImageWidth = fInput.readUnsignedByte() * 256 + sByte;

    fInput.close();
    return kCoords;
    default:
    break;
    }
    //close out this entry
    fInput.close();
    return "Unhandled image type";
    }
    </cfscript>



    </head>
    <body bottommargin="0" leftmargin="0" rightmargin="0" topmargin="0" background="../img/background.jpg">

    <!-------- page data goes below here ------>
    <!--------------->
    <div align="center"><h1><font color="#FF0000">Database Maintenance</font></h1></div>
    <br><br><br>

    <br><br>

    <div align="center"><table border="2" width="100">
    <tr>
    <td>

    <div align="center">Click here to upload a new Event Photo:
    <cfif isdefined("form.upload_now")>
    <cffile action="upload" filefield="ul_path" destination= "C:\CFusionMX7\wwwroot\aasr\eventpics\" nameconflict="overwrite">
    <cfset newfile = #cffile.serverfile#>

    <CFSCRIPT>
    kImageSize = ImageSize("#newfile#");
    </CFSCRIPT>

    <cfquery name="insert_eventpics" datasource="gph">
    insert into eventpics
    (eventname, picpath, imageheight, imagewidth, pictext)
    values
    ('#eventname#', '#newfile#', '#kImageSize.ImageHeight#', '#kImageSize.ImageWidth#', '#pictext#')
    </cfquery>


    </cfif>
    <form action="" method="post" name="upload_form" enctype="multipart/form-data" id="upload_form">
    <input type="File" name="ul_path" size="40" id="ul_path">
    </td>
    </tr>

    <tr>
    <td>
    Event name: <input type="text" name="eventname" id="eventname" size="40">
    </td>
    </tr>
    <tr>
    <td>
    Picture text: <input type="text" name="pictext" id="pictext" size="40">
    </td>
    </tr>
    <tr>
    <td>
    <div align="center"><input type="submit" name="upload_now" value="Submit"></div>
    </td>
    </tr>
    </form>




    </table>

    </div>
    <!-------- page data goes above here ------>
    <!--------------->
    </body>
    </html>


    ghouserAuthor
    Inspiring
    October 2, 2006
    Azadi, I have two files. The first is the form page (uploadEventPic.cfm) and the action page (imagesize.cfm). The need here is to do whatever I can to pass the name of the uploaded file to the action page. No matter what I try I can't seem to push the value of #newfile# to the action page.

    The ultimate goal, if anyone else out there has done this, is to upload a file, then store the name of the file (name.ext only) into a db field with two other fields having the image height and width. I have a script that gives the dimensions. But for some reason the part I thought would be the easiest eludes me, getting the filename. I can't get cffile.serverFilename to work for me.
    Inspiring
    October 2, 2006
    so, is your form page and result page the same page then (imagesize.cfm)?
    the proper setup for file uploading is:
    - a form page with input type=file field where you select a file to upload
    - action page (whihc can be same as form page), which uploads the file through cffile action=upload

    in your code:
    - i do not see a <cfoutput> around #newfile# in your hidden field
    - i do not really understand why you need the name of uploaded file in a hidden form field in a form that uploads a file...

    another possible culprit could be hidden in the XXXX... can you post the value of destination attribute of the cffile tag?
    ghouserAuthor
    Inspiring
    October 1, 2006
    I tried to pass the value of the CF variable via the form with the following code, but it didn't work. I assume either my syntax is wrong or you can't do it this way. suggestions?

    <cfif isdefined("form.upload_now")>
    <cffile action="upload" filefield="ul_path" destination= "XXXX" nameconflict="overwrite">
    <cfset newfile = cffile.serverfile>
    </cfif>
    <form action="imagesize.cfm" method="post" name="upload_form" enctype="multipart/form-data" id="upload_form">
    <input type="File" name="ul_path" size="20" id="ul_path">
    <input type="hidden" name="newfile" id="newfile" value="#newfile#">
    <input type="submit" name="upload_now" value="Submit"></center>

    </form>
    Inspiring
    October 1, 2006
    what do you mean by "result page"? is it a different page than the one your form is on? if so, then you do not have access to your <cffile ...> any more. cffile.serverfile is only available on the same page as cffile action='upload'.
    if you need to use the name of uploaded file on a different page, you can add
    <cfset myvar = cffile.serverfile> line after your <cffile action="upload" ...> line, and then pass myvar to your other page as url or from variable...
    ghouserAuthor
    Inspiring
    October 1, 2006
    Sure did. Here's the code, with the true URL masked. The upload does take place but I don't get the filename on the result page as I mentioned.

    <cfif isdefined("form.upload_now")>
    <cffile action="upload" filefield="ul_path" destination= "XXXX" nameconflict="overwrite">
    </cfif>
    <form action="imagesize.cfm" method="post" name="upload_form" enctype="multipart/form-data" id="upload_form">
    <input type="File" name="ul_path" size="40" id="ul_path">
    <input type="submit" name="upload_now" value="Submit"></center>
    </form>
    Inspiring
    October 1, 2006
    did you actually use <cffile action='upload' ...> to upload your file?