doudie2693 2019-03-11 07:14
浏览 81

如何在html,js,php中制作带上传图片按钮的迷你图片框

I'm still learning to programme. I want to make images box like this:

enter image description here

Button "+" is for upload images. How I can make this design with these functions?

I have some code but it uploads only one image. My HTML code:

<div class="item-card">
  <div class="card-section">
    <div class="clearfix">
      <!---<div class="pull-right">
        <input type="file" name="fileToUpload" id="fileToUpload">
      </div>---->
      <hr />
      <div class="pull-right">
        <input class="upload_file" type="file" name="fileToUpload" id="fileToUpload" style="">
        <div class="item-images clearfix">
          <div class="empty-text">
            Upload images
          </div>
        </div>
      </div>
    </div>
    <hr />
  </div>
</div>

Jquery(which show upload image):

$('#fileToUpload').on('change', function() {

        var file = this.files[0];
        var imagefile = file.type;
        var imageTypes = ["image/jpeg", "image/png", "image/jpg", "image/gif"];
        if (imageTypes.indexOf(imagefile) == -1) {
            //display error
            return false;
            $(this).empty();
        }
        else {
            var reader = new FileReader();
            reader.onload = function(e) {
                $(".empty-text").html('<img src="' + e.target.result + '"  />');
            };
            reader.readAsDataURL(this.files[0]);
        }

    });

And PHP code(which upload the images to database)

if($_FILES["fileToUpload"]["tmp_name"]) {
            $uploadOk = 1;
            //$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
            // Check if image file is a actual image or fake image
            $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
            if($check !== false) {
                $uploadOk = 1;
            } else {
                $uploadOk = 0;
            }
            // Check file size
            if ($_FILES["fileToUpload"]["size"] > 500000) {
                $uploadOk = 0;
            }
            // Allow certain file formats
            /*if($imageFileType != "jpg") {
               $uploadOk = 0;
            }*/
            // Check if $uploadOk is set to 1
            if ($uploadOk == 1) {
                $this->db->insert('products_images', array('product_id' => $product_id));
                $insert = $this->db->insert_id();

                $target_dir = "./" . p_image_path();
                $target_file = $target_dir . '/' . $insert . '.jpg';
                move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);
                $this->db->update("products", array('main_image' => $insert), array('id' => $product_id));

            }
        }

        redirect(site_url('mystore/products/edit/' . $product_id));
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
    • ¥20 Java-Oj-桌布的计算
    • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
    • ¥20 有人知道这种图怎么画吗?
    • ¥15 pyqt6如何引用qrc文件加载里面的的资源
    • ¥15 安卓JNI项目使用lua上的问题
    • ¥20 RL+GNN解决人员排班问题时梯度消失
    • ¥60 要数控稳压电源测试数据
    • ¥15 能帮我写下这个编程吗
    • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路