views:

90

answers:

2

I am trying to write a PHP script that can upload multiple files.

for($i=0;$i<count($_FILES['uploadimg']['name']);$i++){
    $name = $_FILES['uploadimg']['name'][$i];
    $type = $_FILES['uploadimg']['type'][$i];
    $filepath = $_FILES['uploadimg']['tmp_name'][$i];
    $size = getimagesize($filepath);
    $img = file_get_contents($filepath);

    //insert into database
}

The problem is that the variables are not being populated the way they should.

When I upload a jpeg "image.jpg", I have discovered that the variables have the following values: (by replacing database code with echos and var_exports)

$name = 'image.jpg'; // good
$type = ''; // not good
$filepath = ''; // not good
$size = false; // not good
$img = false; // not good

I should note that I realize $size and $img are dependent on $filepath being a valid file path.

Could anyone offer some insight into what is going wrong or what I am missing? I've been playing around with the code for a day now, and cannot come up with a solution.

Update:

print_r($_FILES) on 3 images returns:

Array (
    [uploadimg] => Array (
        [name] => Array (
            [0] => test1.jpg
            [1] => test2.jpg
            [2] => test3.jpg
        )
        [type] => Array (
            [0] => image/jpeg
            [1] =>
            [2] => image/jpeg
        )
        [tmp_name] => Array (
            [0] => /tmp/phpkC6f2F
            [1] =>
            [2] => /tmp/phpgFrPl8
        )
        [error] => Array (
            [0] => 0
            [1] => 1
            [2] => 0
        )
        [size] => Array (
            [0] => 238906
            [1] => 0
            [2] => 237308
        )
    )
)

Can I then assume this means the image is too big?

+2  A: 

This question / answer might be of help.

Edit - It's look like a file size issue. Both the files that worked have sizes of approx 1.8 Mb and by default PHP has a file upload limit of 2 Mb. I can only give you advice from a Windows perspective on this, but I'd be looking in PHP.ini to increase the upload_max_filesize and perhaps post_max_size settings.

Edit 2 - There is a section on PHP.net that lists the error codes for uploading. You're getting a 1 which is:

UPLOAD_ERR_INI_SIZE

Value: 1; The uploaded file exceeds the upload_max_filesize directive in php.ini.

That matches what I said in my first edit.

Pauk
Thanks, that explains a lot, especially in conjunction with Ross's suggestion of doing a dump and it's results which I've now posted.
Austin Hyde
+1  A: 

Are all these files the same type? Can you give us a var_dump of the variables when uploading a few files? Are the variables for all uploads empty or just some?

For example, this code:

<?php
print_r($_FILES);
?>

<html><body>
<form action="test.php" method="post" enctype="multipart/form-data">
    <input name="upload[]" type="file" /><br />
    <input name="upload[]" type="file" /><br />
    <input name="upload[]" type="file" /><br />
    <input type="submit" value="Upload" />    
</form>
</body></html>

Returns this output:

Array (
    [upload] => Array (
        [name] => Array (
            [0] => IMG_0005.jpg
            [1] => IMG_0249.jpg
            [2] => IMG_0007.JPG
        )
        (...snip...)
        [size] => Array ( 
            [0] => 1776529 
            [1] => 1902522 
            [2] => 798008 
        ) 
    ) 
)

Remember to check $_FILES['name']['error'] for each file.

Ross
I've posted the results, and I believe that indicates the problem is the file size.
Austin Hyde