上传并将具有不同输入名称的多个图像插入数据库

时间:2018-02-18 11:45:07

标签: php mysql file-upload multiple-file-upload

对于具有不同输入名称(image1,image2,image3)的图像,我有3个输入字段,我在这里做了前端(https://plnkr.co/edit/RR6XKb5r3CSBfhLrR3Td?p=preview )。请仔细看看,以便更好地理解。

我尝试使用迄今为止获得的知识,将上传单个图像上传到数据库中。但现在我想上传3个图像,其中包含3个不同的输入名称或输入到我的数据库中的属性(image1,image2,image3),这些属性将使用这种方式属于一个唯一的ID,但是当我提交表单时,它不起作用。就像我说的那样,我刚刚使用上传单个图像时学到的知识。

任何人都可以帮助我实现目标吗? 问候。 “表格前的代码”

  if (isset($_POST['submit'])){
    $permited  = array('jpg', 'jpeg', 'png', 'gif');
    $file_name = $file['image1']['name'];
    $file_size = $file['image1']['size'];
    $file_temp = $file['image1']['tmp_name'];

    $file_name2 = $file['image2']['name'];
    $file_size2 = $file['image2']['size'];
    $file_temp2 = $file['image2']['tmp_name'];

    $file_name3 = $file['image3']['name'];
    $file_size3 = $file['image3']['size'];
    $file_temp3 = $file['image3']['tmp_name'];

    $div = explode('.', $file_name);
    $file_ext = strtolower(end($div));
    $unique_image = substr(md5(time()), 0, 10).'.'.$file_ext;
    $uploaded_image = "../uploads/".$unique_image;

    $div2 = explode('.', $file_name2);
    $file_ext2 = strtolower(end($div2));
    $unique_image2 = substr(md5(time()), 0, 10).'.'.$file_ext2;
    $uploaded_image2 = "../uploads/".$unique_image2;

    $div3 = explode('.', $file_name3);
    $file_ext3 = strtolower(end($div3));
    $unique_image3 = substr(md5(time()), 0, 10).'.'.$file_ext3;
    $uploaded_image3 = "../uploads/".$unique_image3

    if($file_name == "" || $file_name2 == "" || $file_name3 == "" || ){
        $msg = "<span class='error'>Fields must not be empty!</span>";
        return $msg;

    } elseif ($file_size >1048567 || $file_size2 >1048567  || $file_size3 >1048567) {
         echo "<span class='error'>Image Size should be less then 1MB!
     </span>";

    } elseif (in_array($file_ext, $permited) === false || in_array($file_ext2, $permited) === false || in_array($file_ext3, $permited) === false ) {
        echo "<span class='error'>You can upload only:-".implode(', ', $permited)."</span>";

    } else{
        move_uploaded_file($file_temp, $uploaded_image);
        move_uploaded_file($file_temp2, $uploaded_image2);
        move_uploaded_file($file_temp3, $uploaded_image3);

        $query = "INSERT INTO product(image, image2, image3) VALUES('$uploaded_image', '$uploaded_image2', '$uploaded_image3')";
                $inserted_row = $this->db->insert($query);

                if($inserted_row){
                        echo "<span class='success'> Your images has been uploaded successfully. </span>";
                    } else{
                        echo "<span class='error'> Sorry! Your images are not added! Try again later. </span>";
                    }
    }
}

0 个答案:

没有答案