SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Evangelist
    Join Date
    Dec 2008
    Location
    Plymouth, United Kingdon
    Posts
    449
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    From Single file input array to Multiple files input array with PHP

    Two problems:

    1. How can I check whether I receive a Single file input array or a Multiple files input array from a form?

    2. How can I change the Single file input array into the Multiple files input array?

    Single file input array,

    PHP Code:
     Array
        (
            [
    file] => Array
                (
                    [
    name] => Around the Shoreline of Plymouth01.jpg
                    
    [type] => image/jpeg
                    
    [tmp_name] => C:\wamp\tmp\php7FC5.tmp
                    
    [error] => 0
                    
    [size] => 258246
                
    )
        
        ) 
    Multiple files input array,


    PHP Code:
    Array
        (
            [
    file] => Array
                (
                    [
    name] => Array
                        (
                            [
    0] => Around the Shoreline of Plymouth01.jpg
                        
    )
        
                    [
    type] => Array
                        (
                            [
    0] => image/jpeg
                        
    )
        
                    [
    tmp_name] => Array
                        (
                            [
    0] => C:\wamp\tmp\phpFF9D.tmp
                        
    )
        
                    [
    error] => Array
                        (
                            [
    0] => 0
                        
    )
        
                    [
    size] => Array
                        (
                            [
    0] => 258246
                        
    )
        
                )
        
        ) 

    This is what I came out with but it is still not correct...


    PHP Code:
     $array = array();
        foreach (
    $_FILES as $key => $fileItSelf
        {
          
            foreach (
    $fileItSelf as $key => $value
            {
                
    $array[] = array($key => $value);
                
    //$value[]
            
    }
            
        }
        
        
    print_r($array); 
    result:

    PHP Code:
     Array
        (
            [
    0] => Array
                (
                    [
    name] => Around the Shoreline of Plymouth01.jpg
                
    )
        
            [
    1] => Array
                (
                    [
    type] => image/jpeg
                
    )
        
            [
    2] => Array
                (
                    [
    tmp_name] => C:\wamp\tmp\php4C50.tmp
                
    )
        
            [
    3] => Array
                (
                    [
    error] => 0
                
    )
        
            [
    4] => Array
                (
                    [
    size] => 258246
                
    )
        
        ) 
    Thanks,
    Lau

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    72 Post(s)
    Tagged
    0 Thread(s)
    is_array will be able to tell you if there's more than one file.

    As far as transformation: Why do you need to? But if you really wanted to:
    PHP Code:
    $values array_values($_FILES['file']);
    foreach(
    array_keys($_FILES['file']) AS $index => $key) {
      
    $out['file'][$key][0] = $values[$index];


  3. #3
    SitePoint Evangelist
    Join Date
    Dec 2008
    Location
    Plymouth, United Kingdon
    Posts
    449
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    is_array will be able to tell you if there's more than one file.

    As far as transformation: Why do you need to? But if you really wanted to:
    PHP Code:
    $values array_values($_FILES['file']);
    foreach(
    array_keys($_FILES['file']) AS $index => $key) {
      
    $out['file'][$key][0] = $values[$index];

    Thanks StarLion,

    Your code returns this,

    PHP Code:
    Array
    (
        [
    file] => Array
            (
                [
    0] => Array
                    (
                        [
    name] => Around the Shoreline of Plymouth01.jpg
                        
    [type] => image/jpeg
                        
    [tmp_name] => C:\wamp\tmp\phpAB90.tmp
                        
    [error] => 0
                        
    [size] => 258246
                    
    )
     
            ) 
    But I actually need it like this,


    PHP Code:
    Array
    (
        [
    file] => Array
            (
                [
    name] => Array
                    (
                        [
    0] => The Smashing Pumpkins
                    
    )
     
                [
    type] => Array
                    (
                        [
    0] => image/jpeg
                    
    )
     
                [
    tmp_name] => Array
                    (
                        [
    0] => C:\wamp\tmp\phpBE71.tmp
                    
    )
               ....
     
            )
     


  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    72 Post(s)
    Tagged
    0 Thread(s)
    No, my code gives exactly what you want. Your input must be malformed.

    You said the input was
    PHP Code:
     Array
        (
            [
    file] => Array
                (
                    [
    name] => Around the Shoreline of Plymouth01.jpg
                    
    [type] => image/jpeg
                    
    [tmp_name] => C:wamptmpphp7FC5.tmp
                    
    [error] => 0
                    
    [size] => 258246
                
    )
        ) 
    So, when I translate this out into PHP to simulate the input:
    PHP Code:
    <?php
    $_FILES 
    =  array('file' => array('name' => "Around the Shoreline of Plymouth01.jpg",'type' => 'image/jpeg','tmp_name' => 'C:wamptmpphpAB90.tmp','error' => 0,'size' => 258246));

    $values array_values($_FILES['file']);
    foreach(
    array_keys($_FILES['file']) AS $index => $key) {
      
    $out['file'][$key][0] = $values[$index];


    print_r($out);
    ?>
    I get:
    Code:
    Array
    (
        [file] => Array
            (
                [name] => Array
                    (
                        [0] => Around the Shoreline of Plymouth01.jpg
                    )
    
                [type] => Array
                    (
                        [0] => image/jpeg
                    )
    
                [tmp_name] => Array
                    (
                        [0] => C:wamptmpphpAB90.tmp
                    )
    
                [error] => Array
                    (
                        [0] => 0
                    )
    
                [size] => Array
                    (
                        [0] => 258246
                    )
    
            )
    
    )
    Which is exactly what you asked for.

  5. #5
    SitePoint Evangelist
    Join Date
    Dec 2008
    Location
    Plymouth, United Kingdon
    Posts
    449
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    No, my code gives exactly what you want. Your input must be malformed.
    Thanks StarLion!

    Got it working as in your code now.

    PHP Code:
    <?php

    $values 
    array_values($_FILES['file']);
    foreach(
    array_keys($_FILES['file']) AS $index => $key) {
      
    $out['file'][$key][0] = $values[$index];


    ?>
    but why it does work like this?

    PHP Code:
    <?php

    $values 
    array_values($_FILES);
    foreach(
    array_keys($_FILES) AS $index => $key) {
      
    $out[$key][0] = $values[$index];


    ?>
    Thanks,
    Lau

  6. #6
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    72 Post(s)
    Tagged
    0 Thread(s)
    Because $_FILES is not the base array that you're trying to walk through - $_FILES['yourformfieldnamehere'] is.

    Clicky linky.
    http://hu.php.net/manual/en/features...ost-method.php

  7. #7
    SitePoint Evangelist
    Join Date
    Dec 2008
    Location
    Plymouth, United Kingdon
    Posts
    449
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    Because $_FILES is not the base array that you're trying to walk through - $_FILES['yourformfieldnamehere'] is.

    Clicky linky.
    PHP: POST method uploads - Manual
    Got it thanks for the help!

    Code:
    is_array will be able to tell you if there's more than one file.
    I still cannot work it out to determine if there's more than one file with is_array - could you please give me some hint?

    Thanks!

  8. #8
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    72 Post(s)
    Tagged
    0 Thread(s)
    if $_FILES['file']['name'] is an array, there are multiple files. If it's just a string, there's only 1 file.

  9. #9
    SitePoint Evangelist
    Join Date
    Dec 2008
    Location
    Plymouth, United Kingdon
    Posts
    449
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    if $_FILES['file']['name'] is an array, there are multiple files. If it's just a string, there's only 1 file.
    Thanks. Got that!

    PHP Code:
    if(!is_array($_FILES['file']['name']))
    {
        
        
    $values array_values($_FILES['file']);
        foreach(
    array_keys($_FILES['file']) as $index => $key
        {
            
    $out['file'][$key][0] = $values[$index];
        } 

        
    print_r($out);


    Thanks so much for the help!


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
  •