Skip to main content
Participating Frequently
June 18, 2006
Answered

<cfif> problems

Hi,

I'm trying to build a coldfusion script that will allow users to upload files. This part works fine, but I'm also trying to get the script to generate the relevant linking tags for the uploaded file, depending on whether it's an image or not.

To do this, I'm currently using a <cfif> statement to check the file extension, like this:

<cfif #CFFILE.ServerFileExt# IS "jpg" OR "bmp" OR "gif" OR "png" OR "jpeg">

When I try to upload an image, it works fine and produces the relevant tags. But when I upload a file that isn't an image, it gives me this error:

cannot convert the value "bmp" to a boolean

I've also tried using the <CFIF IsArray(myarray) IS "condition1" OR "condition2"> statement, which also gave me errors.

Would be grateful if anyone could help.

Thanks.
    This topic is closed to new replies. Start a new post to keep the conversation going.
    SafariTECHCorrect answer
    Inspiring
    June 18, 2006
    Post your entire block of code surrounding the CFIF block ... we can't really help you if we only see a piece of it.


    You could try starting with fixing the current CFIF statement so it evaluates properly:

    <cfif (CFFILE.ServerFileExt IS "jpg") OR (CFFILE.ServerFileExt IS "bmp") OR (CFFILE.ServerFileExt IS "gif") OR (CFFILE.ServerFileExt IS "png") OR (CFFILE.ServerFileExt IS "jpeg")>


    but I suspect this is only the start of the issue, since a non image should skip this statement.



    3 replies

    ms526Author
    Participating Frequently
    June 18, 2006
    Thanks for the help guys, The first suggestion worked perfectly.
    Translate
    Inspiring
    June 18, 2006
    > <cfif #CFFILE.ServerFileExt# IS "jpg" OR "bmp" OR "gif" OR "png" OR "jpeg">

    OR is a binary operator, which takes two operands:

    EG: A OR B

    Where each operand (A, B) must be a boolean value (or evaluate to a boolean
    value).

    So taking the first expression:

    #CFFILE.ServerFileExt# IS "jpg" OR "bmp"

    #CFFILE.ServerFileExt# IS "jpg" evaluates to an boolean value (true or
    false).

    "bmp" does NOT evaluate to a boolean value. Hence the error.

    The reason why it works at all is that CF will "short circuit" series of
    boolean expressions as soon as it gets either a definite true, or a
    definite false.

    So if #CFFILE.ServerFileExt# IS "jpg" evaluates to TRUE, then the entire
    expression must evaluate to true, so it doesn't bother looking at any of
    the other operators (which are all invalid, so just as well).

    Do do what you want, you have at least two options:

    1)
    <cfif CFFILE.ServerFileExt IS "jpg" OR CFFILE.ServerFileExt IS "bmp" OR
    CFFILE.ServerFileExt IS "gif" OR CFFILE.ServerFileExt IS "png" OR
    CFFILE.ServerFileExt IS "jpeg">

    Which is clearly a crappy wat of doing it.

    Or you could do this:

    2)

    <cfif listFindNoCase("jpg,bmp,gif,png,jpeg", CFFILE.ServerFileExt)>

    Clearly this is a bit more managable.

    Note: in neither situation do you need the pound-signs


    > I've also tried using the <CFIF IsArray(myarray) IS "condition1" OR
    > "condition2"> statement, which also gave me errors.

    I don't even know what you're trying to achieve here, so I'll leave this to
    someone else.

    It might be an idea to read some docs, before you get too much further
    along:
    http://livedocs.macromedia.com/coldfusion/7/htmldocs/00000084.htm

    --
    Adam
    Translate
    SafariTECHCorrect answer
    Inspiring
    June 18, 2006
    Post your entire block of code surrounding the CFIF block ... we can't really help you if we only see a piece of it.


    You could try starting with fixing the current CFIF statement so it evaluates properly:

    <cfif (CFFILE.ServerFileExt IS "jpg") OR (CFFILE.ServerFileExt IS "bmp") OR (CFFILE.ServerFileExt IS "gif") OR (CFFILE.ServerFileExt IS "png") OR (CFFILE.ServerFileExt IS "jpeg")>


    but I suspect this is only the start of the issue, since a non image should skip this statement.



    Translate