Skip to main content
December 18, 2009
Question

CFIMAGE misbehaving when an image file has been incorrectly named.

  • December 18, 2009
  • 1 reply
  • 4049 views

ok - really need help on this one - I am stumped....

working on an application that does some simple image manipulation, creates thumbs from an uploaded image.... works mostly perfectly BUT if a user uploads an incorrectly named file <cffile action="upload"> fails.....

for example I have an image named 'myimage.png' the file is actually a jpeg. if I upload that this snippet does nothing, not even throw an error.

<cffile 
     action="upload"
     destination="#basepath##path#"
     fileField="file"
     mode="777"
     nameConflict="makeunique"
     />

I have tailed all the logs [exception/server & server] all I get is a file not found error [it never got uploaded!

I have confirmed that the image file types are actually as I expect them to be - no matter how they are named...

I even updated & patched the server with the latest patch and hotfixes.

and it gets weirder....... if I rename a jpeg to a png file, it will upload, but will fail on a cfimage resize command....

Does anyone have a clue as to what is happening here.

-sean

This topic has been closed for replies.

1 reply

Inspiring
December 19, 2009

That does not sound right. Though I can believe some of the image functions might have a problem with the file extension/image format difference once the files arrive.

Can you post a small reproducible example?

December 19, 2009

it sure does not sound right....  it's downright bizzarre!

but yes - here are the 3 functions involved [I borke them out to try and isolate the problem]

usually this is called from a java applet that allows multiple uploads, but I started testing with a normal form & file field when problems started arising ... no difference so I am assuming the java applet is fine

there's actually a little bit of leftover fluff in these that hasn't been cleaned out yet so... try to ignore it

-thanks

-sean

<!--- resize - resize an image to given directory --->
    <cffunction name="toJpeg" access="public" description="resize an image to given directory" output="yes" returntype="any" >
        <cfargument name="name" required="yes" type="string" />
        <cfargument name="path" required="no" type="string" default=""  />
       
        <cfscript>
            copyPrefix = RandomString('5');
            imageName = left(arguments.name, (Len(arguments.name)-4))&'.jpg';   
        </cfscript>
       
        <cfimage source="#basepath##path##name#" action="write" destination="#basepath##path##imageName#" quality="1" overwrite="yes" />

        <!---cfscript>
            thread = CreateObject("java", "java.lang.Thread");
            thread.sleep(5000);   
        </cfscript--->

    </cffunction>
   
<!--- resize - resize an image to given directory --->
    <cffunction name="resize" access="public" description="resize an image to given directory" output="yes" returntype="any" >
        <cfargument name="name" required="yes" type="string" />
        <cfargument name="newname" required="yes" type="string" />
        <cfargument name="height" required="no" type="string" default="150" />
        <cfargument name="width" required="no" type="string" default="200" />
        <cfargument name="path" required="no" type="string" default=""  />

        <cfimage action="resize" source="#basepath##path##name#" destination="#basepath##path##newname#" width="#width#" height="#height#"  overwrite="yes"  />
    </cffunction>

  
<!--- upload - uploads a file to given directory --->
    <cffunction name="upload" access="public" description="uploads a file to given directory" output="yes" returntype="any" >
        <cfargument name="path" required="no" type="string" default="" />
        <cfargument name="mime" required="no" type="string" default="image/jpg,image/gif,image/png" />
       
        <cfif Find("../","#path#") eq 0>
        <cftry><cfdirectory action="create" directory="#basepath##path#" /><cfcatch type="any"> </cfcatch></cftry>
        </cfif>
       
        <cftry>   
            <cffile action="upload" destination="#basepath##path#" fileField="file" mode="777" nameConflict="makeunique" />
                <cfset error = "false" />
        <cfcatch type="any">
                <cfset error = "true" />
            </cfcatch>
        </cftry>
       
        <cfscript>
            oldname = cffile.serverFile;
            suffix = Right(cffile.serverFile,'4');   
            prefix = RandomString('15');
            bytesize = cffile.fileSize;
            newname = prefix&bytesize&suffix;
            rename = prefix&bytesize&'-tn'&suffix;
            newname=Replace(newname, " ", "", "all");
        </cfscript>
       
        <!---cffile action="rename" source="#basepath##path##cffile.serverFile#" destination="#basepath##path##newname#" /--->
       
        <cfscript>
       
            toJpeg(oldname);
            resize(oldname,prefix&bytesize&'.jpg','480','640');
            resize(oldname,prefix&bytesize&'-tn.jpg','150','200');

        </cfscript>
       
        <!-- delete the original -->
        <cffile action="delete" file="#basepath##path##oldname#" />

        <cfif error is not "false">
            <cfreturn error />
        </cfif>
   
    </cffunction>

Inspiring
December 19, 2009

I would focus on one piece at time. Is the upload function something that can be run independently? It looks like it has some path variables defined elsewhere. Also, what is a sample of a file path that is failing?