【问题标题】:I am unable to upload image file to the mysql database using ajax jquery in php我无法在 php 中使用 ajax jquery 将图像文件上传到 mysql 数据库
【发布时间】:2018-03-06 23:54:09
【问题描述】:

提交表单后,我无法将图像文件上传到 mysql 数据库。我收到错误消息:

未捕获的类型错误:非法调用

在我的检查元素控制台选项卡中。

<!DOCTYPE html>

<html>
<head>
    <title>Ajax Test</title>
    <style>
         .error{
             color: red;
        }
        .success{
            color: green;
        }
        .input-error {
            box-shadow: 0 0 5px red;
        }
        .input-success {
            box-shadow: 0 0 5px green;
        }
    </style>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
    <script>
        $(document).ready(function() {
            $('form').submit(function (event) {
                event.preventDefault();
                var inp = $("#t_input").val();
                var file = $("#t_file").val();
                var sub = $("#t_submit").val();
                $(".test_msg").load("hello_ajax.php", {
                    inp: inp,
                    type: "POST",
                    url: "hello_ajax.php",
                    file: new FormData($(file)[0]),
                    contentType: false,
                    cache: false,
                    processData: false,
                    success: function(feedback){
                        $("#t_file").val("");
                    },
                    sub: sub
                });
            });
        });
        </script>

</head>
<body>
    <h2>Testing Ajax</h2>
    <form action="hello_ajax.php" method="POST">
        <input id="t_input" type="text"  name="tst_input" placeholder="Hello" />
        <input id="t_file" type="file"  name="tst_file"/><br/>
        <input id="t_submit" name="submit" type="submit"  value="submit" /><br/>
        <p class="test_msg"></p>
    </form>
</body>
</html>

下面是重定向后上传文件到mysql数据库的php代码

<?php
    $host = "127.0.0.1";
    $id = "root";
    $pass = "";
    $db = "test_mysql";

    try{
        $conn = new PDO("mysql:host=$host; dbname=$db", $id, $pass);
        $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    }catch(PDOException $e){
        echo "Error" . $e->getMessage();
    }

    if (isset($_POST['sub'])){


        if(isset($_FILES["t_file"])){
            $file = $_FILES['t_file'];
            $sql = "ISERT INTO uploadimage (imgName) VALUES (:img)";
            $stmt->prepare($sql);
            $stmt->bindValue(":img", $file, PDO::PARAM_STR);
            $stmt->execute();
        }else{
            echo "<span class='error'> Unable to input file </span><br/>";
        }

        $in = $_POST["inp"];

        $blk = false;

        if (empty($in)){
            echo "<span class='error'>Field must not be empty! </span>";

            $blk = true;
        }else{
            echo "<span class='success'>" . $in . "Successfull</span>";

        }
    }else{
        echo "<span class='test_msg'>There is some Error </span>";

    }
 ?>
<script>
    $("#test_input").removeClass("input-error");
    var blk = "<?php echo $blk; ?>";

    if(blk == true){
        $("#t_input").addClass("input-error");
        $("#t_input").val("");
    }
    if(blk == false){
        $("#t_input").val('');
    }
</script>

【问题讨论】:

标签: php jquery ajax pdo


【解决方案1】:

$_FILES 数组包含有关文件的元数据,而不是文件本身。实际文件存储在$_FILES['t_file']['tmp_name'] 的临时文件中。您可以使用

获取内容
$file = file_get_content($_FILES['t_file']['tmp_name']);

检查documentation 以获取数组的完整结构。

【讨论】:

  • 您也应该在尝试使用该文件之前检查$_FILES['t_file']['error']
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-12-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-25
  • 1970-01-01
  • 2017-12-07
  • 2014-06-18
相关资源
最近更新 更多