PHP上传多张图片同时插入多个"Select"数据进入Mysql


PHP Upload Multiple Images While Inserting Multiple "Select" Data Into Mysql

我有一个图片上传页面,内容如下:

列出9个类别的多个选择框。一个文本输入字段,表示图像的全部文本将是什么。一个多文件上传字段。

目标是能够一次上传多个图像,存储每个alt文本的文本输入,并且还存储每个选定类别的任何。

我写的脚本不一定失败,因为我得到的错误是一个内置到脚本Sorry, there was a problem uploading your images.

整页在这里:

<?php 
include($_SERVER['DOCUMENT_ROOT'].'/settings/functions.php');
include($_SERVER['DOCUMENT_ROOT'].'/settings/config.php');
include($_SERVER['DOCUMENT_ROOT'].'/php/top-notification.php');
include($_SERVER['DOCUMENT_ROOT'].'/php/nav.php');
?>
<div class="container">
<?php
// Connect to database
$conn = getConnected("lucycypher"); 
// Don't execute upload script unless "upload" is clicked
if(isset($_POST['submit'])) { 
    // This is the directory where images will be saved
    $target = $_SERVER['DOCUMENT_ROOT']."/gallery/";
    // This gets all the other information from the form
    $img_category = implode(',', $_POST['img_category']);
    $img_name = $_POST['img_name'];
    // Count the image files
    if(count($_FILES['img_file']['tmp_name'])) {
        // Loop through each file
        foreach ($_FILES['img_file']['tmp_name'] as $img_file) {
            // moves the image
            if(move_uploaded_file($img_file, $target)) {
                // if upload is a success query data into db 
                mysqli_query($conn, "INSERT INTO gallery_img (img_name, img_category, img_location) VALUES ('$img_name', '$img_category', '$img_file')") ;
                echo '<div class="alert alert-success margin-top">Your imags were successfuly uploaded.</div>';
            }
            else {
                echo '<div class="alert alert-danger margin-top">Sorry, there was a problem uploading your images.</div>';
            }
        }
    }
}
?>
<form class="form-horizontal" method="POST" enctype="multipart/form-data" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
<fieldset>
<!-- Form Name -->
<legend>Upload Image</legend>
<!-- Select Multiple -->
<div class="form-group">
      <label class="col-md-4 control-label" for="img_category">Category</label>
      <div class="col-md-4">
            <select id="img_category" name="img_category[]" class="form-control" multiple="multiple">
                  <option value="horror">Horror</option>
                  <option value="occult">Occult</option>
                  <option value="goth">Goth</option>
                  <option value="industrial">Industrial</option>
                  <option value="fashion">Fashion</option>
                  <option value="fetish">Fetish</option>
                  <option value="avante-garde">Avante-Garde</option>
                  <option value="cosplay">Cosplay</option>
                  <option value="nude">Nude</option>
            </select>
      </div>
</div>
<!-- Text input-->
<div class="form-group">
      <label class="col-md-4 control-label" for="img_name">Name:</label>  
      <div class="col-md-4">
      <input id="img_name" name="img_name" type="text" placeholder="Image Name" class="form-control input-md" required="">
      </div>
</div>
<!-- File Button --> 
<div class="form-group">
      <label class="col-md-4 control-label" for="img_file">Image</label>
      <div class="col-md-4">
            <input id="img_file" name="img_file[]" class="input-file" type="file" multiple>
      </div>
</div>
<!-- Button -->
<div class="form-group">
      <label class="col-md-4 control-label" for="submit">Ready?</label>
      <div class="col-md-4">
            <button type="submit" name="submit" class="btn btn-primary">Upload</button>
      </div>
</div>
</fieldset>
</form>

</div>
<?php include($_SERVER['DOCUMENT_ROOT'].'/php/footer.php'); ?>

和我的数据库布局是:

ID              mediumint(10)   AUTO_INCREMENT
img_name        varchar(50)
img_category    varchar(200)
img_location    blob
img_time        timestamp       on update CURRENT_TIMESTAMP

当我选择多个类别时,选择一个图像文件,并在文本框中输入文本,数据库中不会添加任何内容,图像也不会上传到服务器。

我只是得到Sorry, there was a problem uploading your images.错误。

我的gallery目录有775权限

您的代码中有一些缺陷,例如:

  • 看这一行,

    if(move_uploaded_file($img_file, $target)) { ...
    

    $target应该是指定目标文件名的字符串,不应该是目录路径。

  • 既然您在服务器上上传图像,那么将它们保存在数据库中也是没有意义的。只需在服务器中上传图像并将路径保存在数据库中。因此,将img_location列的数据类型更改为VARCHAR

总的来说,你的代码应该是这样的:
if(isset($_POST['submit'])) { 
    $count = count($_FILES['img_file']['name']);
    for($i = 0; $i < $count; ++$i){
        $target = $_SERVER['DOCUMENT_ROOT']."/gallery/";
        $target = $target . basename($_FILES['img_file']['name'][$i]);
        $img_category = implode(",", $_POST['img_category']);
        $img_name = $_POST['img_name'];
        if(move_uploaded_file($_FILES['img_file']['tmp_name'][$i], $target)){
            mysqli_query($conn, "INSERT INTO gallery_img (img_name, img_category, img_location) VALUES ('$img_name', '$img_category', '$target')") ;
            echo '<div class="alert alert-success margin-top">Your imags were successfuly uploaded.</div>';
        }else {
            echo '<div class="alert alert-danger margin-top">Sorry, there was a problem uploading your images.</div>';
        }
    }
}