Results 1 to 4 of 4

Thread: Uploading multiple files using http

  1. #1
    Join Date
    Aug 2005
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry Uploading multiple files using http

    can someone please tell me how to get my multiple upload working properly...

    I have this in my file.php:

    // Added for PHP with register_globals = off
    if ($sourcefile == "") {
    $sourcefile = $_FILES['sourcefile']['tmp_name'][0];
    $sourcefile_size = $_FILES['sourcefile']['size'][0];
    $sourcefile_name = $_FILES['sourcefile']['name'][0];
    $sourcefile_type = $_FILES['sourcefile']['type'][0];

    $sourcefile = $_FILES['sourcefile']['tmp_name'][1];
    $sourcefile_size = $_FILES['sourcefile']['size'][1];
    $sourcefile_name = $_FILES['sourcefile']['name'][1];
    $sourcefile_type = $_FILES['sourcefile']['type'][1];

    $sourcefile = $_FILES['sourcefile']['tmp_name'][2];
    $sourcefile_size = $_FILES['sourcefile']['size'][2];
    $sourcefile_name = $_FILES['sourcefile']['name'][2];
    $sourcefile_type = $_FILES['sourcefile']['type'][2];

    $sourcefile = $_FILES['sourcefile']['tmp_name'][3];
    $sourcefile_size = $_FILES['sourcefile']['size'][3];
    $sourcefile_name = $_FILES['sourcefile']['name'][3];
    $sourcefile_type = $_FILES['sourcefile']['type'][3];

    }

    And this in my upload_page.inc:

    <tr>
    <td class="body" width="20%">
    <input type=file name="sourcefile[0]" class="Text220" size="100"><br />
    <input type=file name="sourcefile[1]" class="Text220" size="100"><br />
    <input type=file name="sourcefile[2]" class="Text220" size="100"><br />
    <input type=file name="sourcefile[3]" class="Text220" size="100"><br />
    </td>
    </tr>



    It used to be this:

    // Added for PHP with register_globals = off
    if ($sourcefile == "") {
    $sourcefile = $_FILES['sourcefile']['tmp_name'];
    $sourcefile_size = $_FILES['sourcefile']['size'];
    $sourcefile_name = $_FILES['sourcefile']['name'];
    $sourcefile_type = $_FILES['sourcefile']['type'];
    }

    And this:

    <tr>
    <td class="body" width="20%">
    <input type=file name="sourcefile" class="Text220" size="100">
    </td>
    </tr>


    and now it's not working properly?

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    All this
    Code:
     $sourcefile = $_FILES['sourcefile']['tmp_name'][0];
    $sourcefile_size = $_FILES['sourcefile']['size'][0];
    $sourcefile_name = $_FILES['sourcefile']['name'][0];
    $sourcefile_type = $_FILES['sourcefile']['type'][0];
    
    $sourcefile = $_FILES['sourcefile']['tmp_name'][1];
    $sourcefile_size = $_FILES['sourcefile']['size'][1];
    $sourcefile_name = $_FILES['sourcefile']['name'][1];
    $sourcefile_type = $_FILES['sourcefile']['type'][1];
    
    $sourcefile = $_FILES['sourcefile']['tmp_name'][2];
    $sourcefile_size = $_FILES['sourcefile']['size'][2];
    $sourcefile_name = $_FILES['sourcefile']['name'][2];
    $sourcefile_type = $_FILES['sourcefile']['type'][2];
    
    $sourcefile = $_FILES['sourcefile']['tmp_name'][3];
    $sourcefile_size = $_FILES['sourcefile']['size'][3];
    $sourcefile_name = $_FILES['sourcefile']['name'][3];
    $sourcefile_type = $_FILES['sourcefile']['type'][3];
    is nonsense. You're just overwriting the variable with one array item after another, not copying the whole array to the variable.

    In fact, I'm not so sure about putting square brackets in the names. I don't think this will cause it to work as you expect, creating an array. Try renaming the variables to something sensible.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  3. #3
    Join Date
    Aug 2005
    Posts
    16
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Ok I'm now using this instead:


    $numfiles = count($_FILES['sourcefile']['name']);

    for ($i=0; $i<$numfiles; $i++) {
    if (!empty($_FILES['sourcefile']['name'][$i])) {
    $sourcefile = $_FILES['sourcefile']['tmp_name'][$i];
    $sourcefile_size = $_FILES['sourcefile']['size'][$i];
    $sourcefile_name = $_FILES['sourcefile']['name'][$i];
    $sourcefile_type = $_FILES['sourcefile']['type'][$i];

    # process the uploaded file

    }
    }


    but it only uploads the last file in the listing:

    <input type=file name="sourcefile[]" class="Text220" size="100">
    <input type=file name="sourcefile[]" class="Text220" size="100">
    <input type=file name="sourcefile[]" class="Text220" size="100">
    Last edited by SysX; 08-19-2005 at 06:41 AM.

  4. #4
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    That's still no good. Let me break down that loop for you.

    Loop 0:
    Set $sourcefile to $_FILES['sourcefile']['tmp_name'][0]

    Loop 1:
    Set $sourcefile to $_FILES['sourcefile']['tmp_name'][1]

    As you see, this overwrites the previous value, so that you are left with only the last file.

    You should create more variables, or (better) an array.
    Code:
    if (!empty($_FILES['sourcefile']['name'])) {
    $sourcefile = $_FILES['sourcefile']['tmp_name'];
    $sourcefile_size = $_FILES['sourcefile']['size'];
    $sourcefile_name = $_FILES['sourcefile']['name'];
    $sourcefile_type = $_FILES['sourcefile']['type'];
    
    # process the uploaded file
    
    }
    This will give you $sourcefile[0], $sourcefile_size[0]. &c.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

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
  •