Traversing through $_FILES array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    Traversing through $_FILES array

    Ok, so i'm doing the multiple file upload thingymabob!

    I'm just playing around really...

    When i try to check the file type i simply get Array().

    [php]
    foreach ($_FILES['file']['name'] as $_key => $_value)
    {
    echo $_value . " was uploaded successfully.<b r />";
    echo $_FILES['file']['type'] . " <br />";
    }
    [/php]

    So to traverse through the array i added a foreach:

    [php]
    foreach ($_FILES['file']['name'] as $_key => $_value)
    {
    echo $_value . " was uploaded successfully.<b r />";
    foreach($_FILES['file']['type'] as $_x => $_y){
    echo $_value . " is in $_y format<br />";
    }
    }
    [/php]
    But that gives me this:
    Code:
    ut_the_folk_down1.gif was uploaded successfully.
    put_the_folk_down1.gif is in image/gif format
    put_the_folk_down1.gif is in image/jpeg format
    natnme.jpg was uploaded successfully.
    natnme.jpg is in image/gif format
    natnme.jpg is in image/jpeg format
    When i just want:
    Code:
    ut_the_folk_down1.gif was uploaded successfully.
    put_the_folk_down1.gif is in image/gif format
    natnme.jpg was uploaded successfully.
    natnme.jpg is in image/jpeg format
    I'm stumped ...
  • MarkoKlacar
    Recognized Expert Contributor
    • Aug 2007
    • 296

    #2
    Hi,

    I think it's because you loop though the whole array, if you index the array using the filename, $_value, you should only get the filetype for that one.

    Now you're getting the whole content.

    Hope this helps.

    MK

    Comment

    • svenni
      New Member
      • Jan 2008
      • 4

      #3
      Yup. MarkoKlacar is correct. I think you should write something like this instead:

      [PHP]
      foreach ($_FILES as $_key => $_value)
      {
      echo $_FILES[$_key]['name'] . " was uploaded successfully.<b r />";
      echo $_FILES[$_key]['name'] . " is in " . $_FILES[$_key]['type'] . " format<br />";
      }
      [/PHP]
      I'm not quite sure if it will work since I can't test it on this computer. But try it out ;) The syntax might be wrong, but I guess the structure is correct.

      Comment

      • Markus
        Recognized Expert Expert
        • Jun 2007
        • 6092

        #4
        Thanks guys!

        After stirring the logic soup in my brain a little, you guided me into this:

        [php]
        foreach ($_FILES['file']['name'] as $_key => $_value)
        {
        echo $_value . " was uploaded successfully.<b r />";
        echo $_FILES['file']['type'][$_key]."<br />";
        }
        [/php]

        I originally tried to put the key before the ['type'] thinking that the key would have to come immediately after the file array, but i was wrong :P

        Thanks again!

        Comment

        Working...