Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Oct 2008
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Storing multidimensional array into mysql

    I have an array:

    This is the output of:
    PHP Code:
    print_r($_FILES); 
    Code:
    Array
    (
        [pictures] => Array
            (
                [name] => Array
                    (
                        [0] => 001.jpg
                        [1] => 002.jpg
                        [2] => 003.jpg
                        [3] => 004.jpg
                        [4] => 005.jpg
                    )
    
                [type] => Array
                    (
                        [0] => image/jpeg
                        [1] => image/jpeg
                        [2] => image/jpeg
                        [3] => image/jpeg
                        [4] => image/jpeg
                    )
    
                [tmp_name] => Array
                    (
                        [0] => /tmp/phpMX74IR
                        [1] => /tmp/phpkKSkL9
                        [2] => /tmp/php041QAv
                        [3] => /tmp/php8abbHU
                        [4] => /tmp/phpDfIzDn
                    )
    
                [error] => Array
                    (
                        [0] => 0
                        [1] => 0
                        [2] => 0
                        [3] => 0
                        [4] => 0
                    )
    
                [size] => Array
                    (
                        [0] => 46433
                        [1] => 412167
                        [2] => 356231
                        [3] => 417250
                        [4] => 151399
                    )
    
            )
    
    )
    Input fields look like this:
    Code:
    <input type="file" name="pictures[]" />
    <input type="file" name="pictures[]" />
    <input type="file" name="pictures[]" />
    <input type="file" name="pictures[]" />
    I'm trying to grab the attributes of each file and insert them into their respective columns in mysql but I can't figure out how to do that exactly. I've been searching just about everywhere! I mostly work with 1 dimensional arrays and this has got me stumped...

    A single row would consist of:
    ['pictures']['name']['0'], ['pictures']['type']['0'], ['pictures']['tmp_name']['0'], ['pictures']['error']['0'], ['pictures']['size']['0']

    0 is the first file

    This needs to increment for each subsequent row:
    ['pictures']['name']['1'], ['pictures']['type']['1'], ['pictures']['tmp_name']['1'], ['pictures']['error']['1'], ['pictures']['size']['1']

    1 is the second file, etc.

    Sometimes only one file will be uploaded, sometimes a lot of files could be uploaded, my intentions are to not limit the number of files being uploaded.


    Any guidance is much appreciated, thanks!
    Last edited by ccheney; 08-07-2010 at 06:16 AM.

  • #2
    New Coder
    Join Date
    Aug 2010
    Location
    At home with my bunny sleepers.
    Posts
    27
    Thanks
    0
    Thanked 7 Times in 7 Posts
    Try this code, let me know if it works.

    PHP Code:
    if(isset($_FILES['picture']['name']) AND is_array($_FILES['picture']['name']))
    {
        foreach(
    $_FILES['picture']['name'] as $key=>$row)
        {
            
    $name        $row;
            
    $type        $_FILES['picture']['type'][$key];
            
    $tmp_name    $_FILES['picture']['tmp_name'][$key];
            
    $error        $_FILES['picture']['error'][$key];
            
    $size        $_FILES['picture']['size'][$key];
        }


  • Users who have thanked dolrichfortich for this post:

    ccheney (08-07-2010)

  • #3
    New Coder
    Join Date
    Oct 2008
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks a lot!

    I'm getting somewhere at least. I now have data in my table.

    However, it is only inserting the first image attributes the other image data is lost. Also, the:
    PHP Code:
    $_FILES['picture']['type']
    $_FILES['picture']['size'
    Are not inserted for whatever reason.

    Here is my data from the db:
    (imagine this as a row)
    id = 1
    name = 001.jpg
    type= blank
    tmp = /tmp/phpRcUrN4
    error = 0
    size = blank

    My query (probably wrong though?):
    PHP Code:
    mysql_query("INSERT INTO images (name, type, tmp, error, size) VALUES ('$name','$type','$tmp_name','$error','$size')") or die(mysql_error()); 
    I don't think I'm taking advantage of [$key] during my query.
    Last edited by ccheney; 08-07-2010 at 07:02 AM.


  •  

    Tags for this Thread

    Posting Permissions

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