dongxian0421 2015-04-07 17:53
浏览 42
已采纳

上传多个图像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) } }
  • 写回答

1条回答 默认 最新

  • dora1989 2015-04-07 18:47
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效