在php中上传多个图片时出错

时间:2015-06-17 18:43:15

标签: php html

我正在尝试同时上传多张图片,但我遇到了错误。当我选择例如4个图像时,只有4个图像中的一个在数据库中插入4次。请问这里的问题是什么?

这是PHP上传脚本

for ($i = 0; $i < count($_FILES["_photo"]["name"]); $i++) {  
    if (!empty($_FILES['_photo']['name'][$i])) {

    if (($_FILES['_photo']['type'][$i] == 'image/jpeg') OR ($_FILES['_photo']['type'][$i] == 'image/png') OR ($_FILES['_photo']['type'][$i] =='image/gif')) {
        $upload_folder = "./profile_pix/";
        $pic_name = time() . ".jpg";
        $pic_path = $upload_folder . $pic_name;

        require_once "include/resize.php";
        if (move_uploaded_file($_FILES['_photo']['tmp_name'][$i], $pic_path)) {
            $image = new Resize($pic_path);
            $image->resizeImage(180, 180, 'crop');
            $image->saveImage($pic_path);

        }
            $sql2 = "INSERT INTO photos
            (photo, member_id, photo_id)
             VALUES
            ('$pic_name', :session_id, :offer_count)";
            $db -> query($sql2, array('session_id' => $_SESSION['id'], 'offer_count' => $offer_count));
    }else {
            header ("Location: submitoffer.php?err=03");
        }


    }


    else {

                $pic_name_2="default_image.png";
        $pic_path_2 = $upload_folder . $pic_name_2;
        require_once "include/resize.php";
        if (move_uploaded_file($_FILES['_photo']['tmp_name'][$i], $pic_path_2)) {
            $image = new Resize($pic_path_2);
            $image->resizeImage(180, 180, 'crop');
            $image->saveImage($pic_path_2);

        }
            $sql2 = "INSERT INTO photos
            (photo, member_id, photo_id)
             VALUES
            ('$pic_name_2', :session_id, :offer_count)";
            $db -> query($sql2, array('session_id' => $_SESSION['id'], 'offer_count' => $offer_count));

       }

    }  

以下是用于上传照片的HTML表单字段

<input type="file" id="_photo"  name="_photo[]" multiple="multiple">

0 个答案:

没有答案