无法在数据库中上传多个图像

时间:2018-11-19 08:53:41

标签: php html upload

我需要帮助,每次插入多张图像时,它仅显示最后一张已上传的图像,你们可以帮助我解决此问题。

这里是我用于上传和插入数据的代码

<?php
require_once('dbConfig.php');
$upload_dir = '../uploads/';

if(isset($_POST['btnSave'])){
    $judul = $_POST['judul'];
    $deskripsi = $_POST['deskripsi'];
    $status = $_POST['status'];
    $harga = $_POST['harga'];

    $imgName = $_FILES['myfile']['name'];
    $imgTmp = $_FILES['myfile']['tmp_name'];
    $imgSize = $_FILES['myfile']['size'];

    if(empty($judul)){
        $errorMsg = 'Please input judul';
    }elseif(empty($deskripsi)){
        $errorMsg = 'Please input deskripsi';
    }elseif(empty($status)){
        $errorMsg = ' Input status';
    }elseif(empty($harga)){
        $errorMsg = 'Input Harga';
    }elseif(empty($imgName)){
        $errorMsg = 'Please select gambar';
    }else{
        $imgExt = strtolower(pathinfo($imgName, PATHINFO_EXTENSION));
        $allowExt  = array('jpeg', 'jpg', 'png', 'gif');
        $userPic = time().'_'.rand(1000,9999).'.'.$imgExt;
        if(in_array($imgExt, $allowExt)){
            if($imgSize < 5000000){
                move_uploaded_file($imgTmp ,$upload_dir.$userPic);
            }else{
                $errorMsg = 'Image too large';
            }
        }else{
            $errorMsg = 'Please select a valid image';
        }
    }

    if(!isset($errorMsg)){
        $sql = "insert into rumah(judul, deskripsi, status, harga, gambar1)
                values('".$judul."', '".$deskripsi."', '".$status."', '".$harga."', '".$userPic."')";
        $result = mysqli_query($conn, $sql);
        if($result){
            $successMsg = 'New record added successfully';
            header('refresh:5;index.php');
        }else{
            $errorMsg = 'Error '.mysqli_error($conn);
        }
    }

} ?> 

上传按钮

<input type="file" name="myfile" multiple>

我已经在表单操作中使用了此方法-enctype =“ multipart / form-data”

0 个答案:

没有答案