多图像上传问题

My HTML code is;

<form action="post_photo.php" method="post" enctype="multipart/form-data">
    <tr>
        <td class="trow1" width="10%">Select Image:</td>
        <td class="trow1" width="90%"><input type="file" name="image[]" multiple="multiple" tabindex="1"></td>
    </tr>
    <tr>
        <td class="trow2" align="left">
            <input type="hidden" name="action" value="do_post_photo" />
            <input type="hidden" name="uid" value="{$uid}" />
            <input type="submit" class="button" name="submit" value="Upload Photos" tabindex="2">
        </td>
    </tr>
</form>

And my post_photo.php file has this code;

if(isset($_FILES['image']))
{
    foreach($_FILES['image']['tmp_name'] as $key => $tmp_name )
    {
        $file_size = $_FILES['image']['size'][$key];
        $file_tmp = $_FILES['image']['tmp_name'][$key];

        $ext = get_extension($_FILES['image']['name'][$key]);
        $allowed_ext = explode(",","png,gif,jpg,jpeg");
        if(!in_array($ext,$allowed_ext))
        {
            error("Invalid extension.");
        }

        if($file_size > 2097152)
        {
            error("File size must be less than 2 MB.");
        }       
        $file_name = "photo_".TIME_NOW.".".$ext;
        $insert_array = array(
            "uid" => $uid,
            "image" => $file_name,
            "approved" => '1',
            "likes" => '',
            "dateline" => time()
        );

        if(is_dir("uploads/photos/".$file_name)==false)
        {
            move_uploaded_file($file_tmp,"uploads/photos/".$file_name);
        }
        $db->insert_query("photos", $insert_array);
    }
}
redirect("post_photo.php?action=post_photo", "Photos have been posted successfully.");

The issue is, if I select 4 images it upload 2 images to the ./uploads/photos/ folder. However it inserts 4 records in the photos sql table (which is correct since I selected 4 images) and all those records are correct too (I mean all those records have separate data for each image which is correct). And if I select 2 images then it uploads only one to ./uploads/photos/ folder however it inserts 2 records in sql table.

Why it is showing this issue? Please help!

Change your PHP code to this;

if(isset($_FILES['image']))
{
    $count = 0;
    foreach($_FILES['image']['name'] as $key => $tmp_name)
    {
        $file_size = $_FILES['image']['size'][$key];
        $file_tmp = $_FILES['image']['tmp_name'][$key];

        $ext = get_extension($_FILES['image']['name'][$key]);
        $allowed_ext = explode(",","png,gif,jpg,jpeg");
        if(!in_array($ext,$allowed_ext))
        {
            error("Invalid extension.");
            continue;
        }

        if($file_size > 2097152)
        {
            $size_in_kb = $file_size/1000;
            error("File size must be less than 2MB.");
            continue;
        }

        if(move_uploaded_file($file_tmp,"uploads/photos/".$tmp_name))
        {
            $insert_array = array("uid" => $uid,    "image" => $tmp_name,   "approved" => '1',  "likes" => '',  "dateline" => TIME_NOW);
            $db->insert_query("photos", $insert_array);
            $count++;
        }
    }
}
redirect("post_photo.php?action=post_photo", "Photos have been posted successfully.");

It should work!

You said that 4 records are added to the db and only 2 images are uploaded. Here's the problem:

    if(is_dir("uploads/photos/".$file_name)==false)
    {
        //1
        move_uploaded_file($file_tmp,"uploads/photos/".$file_name);
    }
    //2
    $db->insert_query("photos", $insert_array);

As you can see, step //2 is always executed, but the step //1 is executed only if this condition is true: "is_dir("uploads/photos/".$file_name)==false".

Solution: Check if(move_uploaded_file (......)) before executing the sql statement.