繁体   English   中英

上传多张图片PHP

[英]Uploading multiple images PHP

我正在尝试上传多个图像和图像描述,但似乎无法弄清为什么它不能正常工作。

我有一个表单,在该表单中有一个看起来像这样的表:

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

此处的按钮将仅向表中添加另一行,该行与<tbody><tr>相同,从而允许用户添加带有说明的多重图像。

当用户单击我的表单提交按钮时,它将尝试遍历每张照片,并将图像上载到我网站内的图像目录,然后将目录路径和描述信息插入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语句而感到困惑,我使用的图像都是小尺寸的.jpg图像,我也在其他地方使用了该图像进行测试。

有任何想法吗?

谢谢

编辑

我已经包含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) } }

好吧,我认为这对您有用...

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个数组组成的数组,每个数组包含两个元素。 您需要确保为它们使用正确的索引。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM