file upload checking

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gryffin
    New Member
    • May 2006
    • 1

    file upload checking

    im trying to do file extension checking but its not working :(

    i have the following in the head

    Code:
    <script language="JavaScript">
        extArray = new Array(".jpg", ".png", ".bmp");
        function LimitAttach(form, file) {
        allowSubmit = false;
        if (!file) return;
        while (file.indexOf("\\") != -1)
        file = file.slice(file.indexOf("\\") + 1);
        ext = file.slice(file.indexOf(".")).toLowerCase();
        for (var i = 0; i < extArray.length; i++) {
        if (extArray[i] == ext) { allowSubmit = true; break; }
        }
        if (allowSubmit) return true;
        else
        alert("Please only upload files that end in types:  "
        + (extArray.join("  ")) + "\nPlease select a new "
        + "file to upload and submit again.");
        return false;
        }
        //  End -->
    </script>
    this is the html code
    the {} are things inserted via php
    i believe it is something to do with the onclick

    [HTML] <form name="upload" action="{THIS}" method="post" enctype="multip art/form-data">
    <input type="hidden" name="MAX_FILE_ SIZE" value="6510200" >
    <input type="hidden" name="op" value="upload" />
    <input type="hidden" name="dir" value="{NEXT_DI R}" />
    <table border="0" width="100%" cellpadding="1" >
    <tr class="task_cel l">
    <td width="5%" nowrap="nowrap" ><b>{LOCAL}</b>&nbsp;</td>
    <td width="70%"><in put type="file" name="local" size="45" style="width: 90%" onchange="javas cript:filename( );" /></td>
    <td valign="bottom" align="right">< input onclick="javasc ript: window.location .href = '{MULTI_UPLOAD} ?dir={NEXT_DIR} ';" type="button" value="{MULTIPL E}" /></td>
    </tr>
    <tr class="task_cel l">
    <td width="5%" nowrap="nowrap" ><b>{UPNAME}</b>&nbsp;</td>
    <td width="70%"><in put type="text" name="remote" size="45" style="width: 90%" /></td>
    <td align="right" valign="bottom" ><input onclick="javasc ript: return isValid(); return LimitAttach(thi s.form, this.form.local .value);" type="submit" value="{UPLOAD} " /></td>
    </tr>
    </table>
    </form>[/HTML]

    if anyone knows whats wrong please help, i will even pay you if you dont need too much as my site cant survive atm without it :)

    [HTML]<input onclick="javasc ript: return isValid(); return LimitAttach(thi s.form, this.form.local .value);" type="submit" value="{UPLOAD} " /> [/HTML]

    is most likley where im going wrong
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    LimitAttach will never run because there is already a return when calling isValid(). Either put both in a separate function and call that or change the return into an if-else statement, so if it's valid, LimitAttach is called, e.g.[HTML]<input onclick="if (isValid()) return LimitAttach(thi s.form, this.form.local .value); else return false;" type="submit" value="{UPLOAD} " /> [/HTML]It's better calling this from form onsubmit rather than submit button onclick.

    Comment

    Working...