BRBT
BRBT

Reputation: 1487

Uploading multiple images PHP

I am trying to upload multiple images and image descriptions and can't seem to figure out why it is not working properly.

I have a form and within that form there is a table that looks like this:

<div id="section_photos">
    <fieldset>
        <table id="photo_table">
            <thead>
                <tr>
                    <th>Roof Section Photo</th>
                    <th>Photo Description</th>
                </tr>
            </thead>
            <tbody id="photo_tb">
                <tr>
                    <td><input type="file" id="roof_section_photo" name="roof_section_photo[]" /></td>
                    <td><input id="section_photo_desc" type="text" name="section_photo_desc[]"/></td> 
                </tr>
            </tbody>
        </table>
        <input type="button" value="Add Photo" id="newSectionPhoto" />
    </fieldset>
</div>

The button here will just add another row to the table that is identical to the <tr> in the <tbody> allowing the user to add muliple images with descriptions.

When the user clicks on my forms submit button it will try to iterate through each photo and upload the image to a image directory within my website, and insert the directory path, and description information to a MySQL DB.

$sectionPhoto = "../images/". $selectedAssocAccount ."/" . $facilityID . "/"
                . basename($_FILES["roof_section_photo"]["name"]); 

foreach($_FILES['roof_section_photo']['name'] as $index => $image){
    $sectionPhotoDesc = $_POST['section_photo_desc'][$index];

    if($_FILES['roof_section_photo']['error'] == UPLOAD_ERR_OK) {
        $status_msg = '';
        $from = $_FILES["roof_section_photo"]["tmp_name"];
        $saved = save_section_photo($from, $sectionPhoto, $status_msg);

        $sectionPhotoQuery = "INSERT INTO table(facility_section_information_id, photo, photo_desc) "
                . "VALUES ('$facilitySectionID', '$image', '$sectionPhotoDesc')";

        //using this to test my query before I submit anything
        echo $sectionPhotoQuery; 
        echo "</br>";

        //mysqli_query($dbc, $sectionPhotoQuery)or die(mysqli_error($dbc));

    } else{
        //THIS IS WHERE I KEEP HITTING..
        echo "Error uploading photo. " . $_FILES['roof_section_photo']['error'];  
    }
}

Output: Error uploading photo. Array

Let's say I have 3 images, I will get my error message 3 times, so I know at least each image is being put into an array and it is being iterated through.

(I dont think I need to post my save_section_photo() function because it's not even getting that far.)

I am using this exact same process to upload just a single image elsewhere without any issue, but it seems to be giving me problems when I try to use it this way.

I am stumped as to why I keep breaking off on my if statement, the images I am using are all small sized, .jpg's that I have also used elsewhere for testing.

Any ideas?

Thanks

EDIT

I have included var_dump( $_FILES['roof_section_photo'] ); before my foreach loop.

Output

array(5) { ["name"]=> array(2) { [0]=> string(12) "einstein.jpg" [1]=> string(10) "monkey.jpg" } ["type"]=> array(2) { [0]=> string(10) "image/jpeg" [1]=> string(10) "image/jpeg" } ["tmp_name"]=> array(2) { [0]=> string(14) "/tmp/php89CrOW" [1]=> string(14) "/tmp/phpPGz7Z9" } ["error"]=> array(2) { [0]=> int(0) [1]=> int(0) } ["size"]=> array(2) { [0]=> int(4798) [1]=> int(3254) } }

Upvotes: 0

Views: 100

Answers (1)

BA_Webimax
BA_Webimax

Reputation: 2679

OK, I think this will work for you...

foreach($_FILES['roof_section_photo']['name'] as $index => $image){
    $sectionPhotoDesc = $_POST['section_photo_desc'][$index];

    if($_FILES['roof_section_photo']['error'][$index] == UPLOAD_ERR_OK) {
        $status_msg = '';
        $from = $_FILES["roof_section_photo"]["tmp_name"][$index];
        $saved = save_section_photo($from, $sectionPhoto, $status_msg);

        $sectionPhotoQuery = "INSERT INTO table(facility_section_information_id, photo, photo_desc) "
                . "VALUES ('$facilitySectionID', '$image', '$sectionPhotoDesc')";

        //using this to test my query before I submit anything
        echo $sectionPhotoQuery;
        echo "</br>";

        //mysqli_query($dbc, $sectionPhotoQuery)or die(mysqli_error($dbc));

    } else{
        //THIS IS WHERE I KEEP HITTING..
        echo "Error uploading photo. " . $_FILES['roof_section_photo']['error'][$index];
    }
}

Since you are looping on the $_FILES['roof_section_photo']['name'] array that stands apart from the $_FILES['roof_section_photo'] array which is an array of 5 arrays with two elements each. You need to make sure you are using the correct index for them.

Upvotes: 1

Related Questions