SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Oct 2002
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    validating multiple uploads - need help!

    Ok, when uploading multiple files with php, one uses an array for the name of the file field like this:

    <input name="image[]" type="file" id="1" size="40">

    Now I have some genius jscript that checks the file types of multiple upload fields. But it requires the name of each of the fields to be unique. Since all my fields have image[] for the name - the script breaks. If I name my fields with image1, image2, image3 etc, the jscript works.

    In order to upload multiples, php requires the name of the field to be an array as above. I just need a little tweak to this jscript!

    Here is the jscript. pretty simple, except for when the field is called using it's "name" property midway down. I tried using unique id's but this didn't seem to work - of course I might have messed it up.

    <script language="JavaScript">

    // this tests that file must be of certain type
    function testForFileExtensions(myForm,extensions)
    {
    var objReg;
    var extension;
    var strErrorMsg = "."+extensions[extensions.length-1];

    var field_Name;
    var doneMsg = false;
    var overallOK = true;
    var ok = false;
    (extensions.length>1)?strErrorMsg=" or "+strErrorMsg:strErrorMsg+=" "; //while?
    for (j=0;j<myForm.elements.length;j++) //for each file upload element
    {
    if (myForm.elements[j].type=="file" && myForm.elements[j].value != "") //if the form element is type fiel and it has an image
    {


    ok = false;
    field_Name = myForm.elements[j].name; //This is the problem IMHO
    for (i=0;i<extensions.length;i++)
    {
    extension = extensions[i];
    // objReg = "/\."+extension+"$/i.test(myForm."+field_Name+".value)";
    objReg = "/\."+extension+"$/i.test(myForm."+field_Name+".value)";

    if ((eval(objReg))) ok = true;


    if (i<extensions.length-1 && !doneMsg) strErrorMsg = "."+extension+","+strErrorMsg; //provide information on the error
    }
    doneMsg = true;
    if (!ok) overallOK = false;
    }
    }
    if (!overallOK)
    {
    alert("Please ensure all files are of type "+strErrorMsg);
    myForm.headline.focus();
    return false;
    } return true;
    }
    </script>


    Please help me!!!!!!! How can avoid using the name property of the file upload element and still address the extension of the field?

  2. #2
    SitePoint Zealot jonsteele's Avatar
    Join Date
    Jan 2001
    Location
    Toronto
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would suggest validating the image type on the serverside using PHP before processing the form.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •