我有下面的代码,我似乎无法弄清楚为什么我的查询失败。有人知道为什么吗?
我尝试了许多在网上找到的建议解决方案,但没有一个起作用。

HTML表单代码遵循PHP代码

<?php include 'connection.php';?>

<?php
    $dir           = substr(uniqid(),-7);
    $valid_formats = array("jpg", "png", "gif", "jpeg");
    $max_file_size = 1024*100; //100 kb

    /*
       $path = "Prototype/uploads/"; // Upload directory
       mkdir ($path, 0744);\
    */

    $count = 0;

    if (isset($_POST['search'])) {
        // Loop $_FILES to exeicute all files
        foreach ($_FILES['files']['name'] as $f => $name) {
            echo "$name--";
            if ($_FILES['files']['error'][$f] == 4) {
                continue; // Skip file if any error found
                echo "something <br>";
            }

            if ($_FILES['files']['error'][$f] == 0) {
                if ($_FILES['files']['size'][$f] > $max_file_size) {
                    $message[] = "$name is too large!.";
                    echo "something***************** <br>";
                    continue; // Skip large files
                } elseif (! in_array(pathinfo($name, PATHINFO_EXTENSION), $valid_formats)) {
                    $message[] = "$name is not a valid format";
                    echo "something+++++++++++++++++++ <br>";
                    echo "$name-- ";
                    continue; // Skip invalid file formats
                } else { // No error found! Move uploaded files
                    // if(move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path.$name))
                    // $count=$count+1; // Number of successfully uploaded file
                    //echo $path.$name;

                    $image = addslashes(file_get_contents($_FILES['files']['tmp_name'][$f]));
                    $image_name = addslashes($_FILES['files']['name'][$f]);
                    $query2 = "Insert into $dbname.Image (Image, ImageName) VALUES ('$image', '$image_name')";
                    $result2 = mysqli_query($conn,$query2);

                    if (!$result2) {
                        echo "ERRORS";
                    }

                    //Number of successfully uploaded file

                }
            }
        }
        echo "$count files were imported";
    }

    //show success message
    /*
        echo "<h1>Uploaded:</h1>";

        if(is_array($files)){
            echo "<ul>";
            foreach($files as $file){
                echo "<li>$file</li>";
            }
            echo "</ul>";
        }
    */
?>

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post" enctype="multipart/form-data">
  <label class="btn btn-primary" for="my-file-selector">
    <input id="my-file-selector" type="file"  name="files[]" style="display:none;" multiple onchange="$('#upload-file-info').html($(this).val());">
    Browse
  </label>
  <span class='label label-info' id="upload-file-info"></span>
  <div style="float:right;">
    <label class="btn btn-primary" for="my-file-selector2">
      <input id="my-file-selector2" type="Submit" style="display:none;" name="search">
      Save
    </label>
  </div>
</form>

最佳答案

我建议您不要将图像直接保存到数据库。

相反,您可以将文件移动到S3之类的云服务中,并将该URL存储在数据库中。

如果不是S3,请将该图像上传到同一项目中的任何文件夹并存储该URL。

是否有将二进制映像存储在数据库中的特定原因?

关于php - 使用PHP上传多个图像,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40401820/

10-12 15:00
查看更多