将图像插入SQL数据库

时间:2017-06-04 14:41:38

标签: php

我正在尝试将图像输入到localhost数据库,但我的代码拒绝合作。 “database [2] .php”部分是PDO连接。数据库的值是id,image和name,数据库称为image。

<form action="image.php" method="post" enctype="multipart/form-data">
    <input type="file" name="image" class="inputBox">
    <input type="submit" class="inputBox"  value="go.">
</form>
<?php

include 'database[2].php';


if(isset($_POST['submit'])){
    if(getimagesize($_FILES['image']['tmp_name']) === FALSE){
        echo "<div class='titleBox'>Please select an image.</div>";
    } else {
        $image = addslashes($_FILES['image']['tmp_name']);
        $name = addslashes($_FILES['image']['tmp_name']);
        $image = file_get_contents($image);
        $image = base64_encode($image);
        saveImage($name, $image);
    }
}

function saveImage($name, $image) {

    $sql = "INSERT INTO image (name, image) VALUES (:name, :image)";
    $stmt = $conn->prepare($sql);

    $stmt->bindParam(':image', $_POST['image']);
    $stmt->bindParam(':name', $_POST['name']);

    if($stmt->execute()):
        echo "<div class='titleBox'>Image uploaded.</div>";
    else:
        echo "<div class='titleBox'>Image not uploaded.</div>";
    endif;

}
?>

0 个答案:

没有答案