上传多个图像PHP

时间:2015-04-07 17:53:16

标签: php html image

我正在尝试上传多张图片和图片说明,似乎无法弄清楚它无法正常工作的原因。

我有一个表单,在该表单中有一个表格如下:

<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>

此处的按钮只会在表格中添加另一行,与<tr>中的<tbody>相同,允许用户添加带描述的多个图像。

当用户点击我的表单提交按钮时,它将尝试遍历每张照片并将图像上传到我网站中的图像目录,并将目录路径和描述信息插入MySQL数据库。

$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'];  
    }
}

输出 上传照片时出错。阵列

我们说我有3张图片,我会收到3次错误信息,所以我知道至少每张图片都被放入一个数组中并且正在迭代。

(我不认为我需要发布我的save_section_photo()功能,因为它甚至没有达到那么远。)

我正在使用这个完全相同的过程在其他地方上传一个图像而没有任何问题,但是当我尝试以这种方式使用它时似乎给我带来了问题。

我很难过为什么我一直在我的if声明中断,我使用的图像都是小尺寸的,我还在其他地方用于测试。

有什么想法吗?

由于

修改

我在var_dump( $_FILES['roof_section_photo'] );循环之前添加了foreach

输出

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) } }

1 个答案:

答案 0 :(得分:1)

好的,我认为这对你有用......

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];
    }
}

由于您在$_FILES['roof_section_photo']['name']数组上循环,该数组与$_FILES['roof_section_photo']数组不同,后者是一个包含5个数组的数组,每个数组包含两个元素。您需要确保使用正确的索引。

相关问题