首页 文章

图片无法正常上传

提问于
浏览
3

我尝试使用 php 管理员创建一个库可以上传多个图像所有图像都正确上传,就像图像在数据库中保存尽可能多,但问题是图像在文件夹中保存一次

像循环运行一样,图像名称在数据库中保存两次

Forexampl img1.jpg img2.jpg img3.jpg

这是数据库中的内容,但当我打开文件夹时,我看到 img1.jpg 其余的都没有来

这是我的代码

$imggall = $_FILES["gallery_img"]["name"];
        $imgtype = $_FILES["gallery_img"]["type"];
        $imgtemp = $_FILES["gallery_img"]["tmp_name"];
        foreach($imggall as $key => $imgname) {
            $path = "./img/gallery/".$imgname;
            foreach($imgtemp as $key1 => $imgtemp1) {
                move_uploaded_file($imgtemp1, $path);
            }
            $confirm = mysqli_query($connect, "INSERT INTO gallery (image) VALUES ('$imgname')");
        }

这是页面的 HTML 表单 post.php

<p>
            <form method="POST" action="post.php?id=<?php echo $_SESSION["id"]; ?>" enctype="multipart/form-data">

            <div class="form-group">
                <label>Title</label>
                <input class="form-control" name="title">
            </div>

            <div class="form-group">
                <label>Category</label>
                <div class="checkbox">
                    <?php
                        $query = mysqli_query($connection, "SELECT DISTINCT cat_name FROM category");
                        while($row = mysqli_fetch_assoc($query)) {
                            if(!empty($row["cat_name"])) {
                    ?>

                    <label><input type="checkbox" name='catname' value="<?php echo $row["cat_name"]; ?>"><?php echo $row["cat_name"]; ?></label>

                    <?php } } ?>

                    <?php
                        $query1 = mysqli_query($connection, "SELECT DISTINCT parent FROM category");
                        while($row1 = mysqli_fetch_assoc($query1)) {
                            if($row1["parent"] !== "None") {
                    ?>

                    <label><input type="checkbox" name='catname' value="<?php echo $row1["parent"]; ?>"><?php echo $row1["parent"]; ?></label>

                    <?php } } ?>
                </div>
            </div>

            <div class="form-group">
                <label>Featured Image</label>
                <input type="file" name="ft_img">
            </div>

            <div class="form-group">
                <label>Description</label>
                <textarea class="form-control" name="descrip" rows="6"></textarea>
            </div>

            <div class="form-group">
                <label>Featured</label>
                <div class="checkbox">
                    <label><input type="checkbox" name='fetch' value="Featured">Featured</label>
                </div>
            </div>

            <div class="form-group">
                <label>Android Button</label>
                <select class="form-control" name="android">
                    <option>Yes</option>
                    <option>No</option>
                </select>
            </div>

            <div class="form-group">
                <label>Iphone Button</label>
                <select class="form-control" name="iphone">
                    <option>Yes</option>
                    <option>No</option>
                </select>
            </div>

            <div class="form-group">
                <label>Custom Button Text</label>
                <input class="form-control" name="cbtn_text">
            </div>

            <div class="form-group">
                <label>Custom Button Link</label>
                <input class="form-control" name="cbtn_url">
            </div>

            <script type="text/javascript">
            $(document).ready(function() {
                var max_fields      = 10; //maximum input boxes allowed
                var wrapper         = $(".input_fields_wrap"); //Fields wrapper
                var add_button      = $(".add_field_button"); //Add button ID

                var x = 1; //initlal text box count
                $(add_button).click(function(e){ //on add input button click
                    e.preventDefault();
                    if(x < max_fields){ //max input box allowed
                        x++; //text box increment
                        $(wrapper).append('<div class="form-group"><input type="file" name="gallery_img[]" style="float:left;" /><a href="#" class="remove_field"><i class="fa fa-times"></i></a><div class="clear"></div></div>'); //add input box
                    }
                });

                $(wrapper).on("click",".remove_field", function(e){ //user click on remove text
                    e.preventDefault(); $(this).parent('div').remove(); x--;
                })
            });

            </script>

            <div class="input_fields_wrap form-group">
                <div class="form-group">
                <button class="add_field_button btn btn-default">Add More Fields</button>
                </div>

                <div class="form-group">
                <label>Gallery Images</label>
                <input type="file" name="gallery_img[]">
                <div class="clear"></div>
                </div>
            </div>

            <div class="form-group">
                <input type="submit" class="btn btn-default purple-btn" value="Submit" name="posts" />
                <input type="Reset" class="btn btn-default purple-btn" value="Reset Value" />
                <a href="cat.php?id=<?php echo $_SESSION['id'];?>" class="btn btn-danger">Cancel</a>
            </div>

            </form>
            </p>

1 回答

  • 1

    循环两次可能会影响性能,这段代码在我的画廊中适合我

    <?php 
        $files = $_FILES['gallery_img']['name'];
        $fileCount = count($files);
        for($i=0; $i<$fileCount; $i++) {
           $tmpFilePath = $files[$i];
           if ($tmpFilePath != ""){
               $newFilePath = "./img/gallery/" . $files[$i];
              if(move_uploaded_file($tmpFilePath, $newFilePath)) {
                // if files are uploaded then only insert into database
                 $confirm = mysqli_query($connect, "INSERT INTO gallery (image) VALUES ('$files[$i]')");
              }
          }
        }
       ?>
    

    希望这可行

相关问题