php - 上传图片到MySQL数据库

标签 php mysql image-uploading

我正在使用此脚本尝试将图像与表单一起上传到我的数据库中。问题是,当我在查询中包含 $cover 时,$insert 的值为 false。谁能告诉我我做错了什么?

    <?php
    session_start();
    $con = mysqli_connect("localhost", "root", "", "testdb") or die("Error " . mysqli_error($con));

    $title = "";
    $year = "";
    $director = "";
    $genre = "";
    $duration = "";
    $description = "";
    $name = "";

    $error = false;

    //check if form is submitted
    if (isset($_POST['addmovie'])) {
        $title = mysqli_real_escape_string($con, $_POST['title']);
        $year = mysqli_real_escape_string($con, $_POST['year']);
        $director = mysqli_real_escape_string($con, $_POST['director']);
        $genre = mysqli_real_escape_string($con, $_POST['genre']);
        $duration = mysqli_real_escape_string($con, $_POST['duration']);
        $description = mysqli_real_escape_string($con, $_POST['description']);
        $file =  mysqli_real_escape_string($con, $_FILES['cover']['tmp_name']);

    //name can contain only alpha characters and space
    if (!preg_match("/^[a-zA-Z ]+$/",$title)) {
        $error = true;
        $title_error = "Name input must contain only alphabets and space";
    }
    if (!preg_match('/^[0-9]+$/',$year)) {
        $error = true;
        $year_error = "Year input must be only numbers";
    }
    if (!preg_match("/^[a-zA-Z ]+$/",$director)) {
        $error = true;
        $director_error = "Director input must contain only alphabets and space";
    }
    if(!preg_match("/^[a-zA-Z ]+$/",$genre)) {
        $error = true;
        $genre_error = "Genre input must contain only alphabets";
    }
    if(!preg_match('/^[0-9]+$/',$duration)) {
        $error = true;
        $duration_error = "Duration input must be only numbers";
    }
    if(!preg_match("/^[a-zA-Z ]+$/",$description)) {
        $error = true;
        $description_error = "Description input must be only letter and numbers";
    } 

    if(!isset($file)) {
        $error = true;
        $cover_error = "Please select an image";
    }else{
        $cover = file_get_contents($_FILES['cover']['tmp_name']);
        $cover_name = $_FILES['cover']['name'];
        $cover_size = getimagesize($_FILES['cover']['tmp_name']);

        if($cover_size == false){
            $error = true;
            $cover_error = "that's not an image";
        }
    }

    if (!$error) {
        if($insert = mysqli_query($con,"INSERT INTO movies(title,d,director,genre,duration,description,cover,cover_name) VALUES('$title','$year','$director','$genre','$duration','$description','$cover','$cover_name')")) {
            $successmsg = "Movie ".$title." scuccesfully uploaded!";
        } else {
            $errormsg = "Cannot upload image!";
        }
    }
}

?>

最佳答案

像这样加载后将文件转换为字符格式;

  $cover = base64_encode($cover);

插入之前。您还应该在 sql 语句中使用绑定(bind)。

关于php - 上传图片到MySQL数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38001335/

相关文章:

php - 如何获取数据表中数据行的ID值?

php - SELECT * FROM 给出其他结果,然后 SELECT 2010,2011 FROM

javascript - 如何在 AngularJS 中使用 CKEditor 上传图像?

java - java中如何上传图片文件

php - 我的递归函数中的问题

php - 如何从 CakeEmails 中删除 CakePHP 公司 "This email was generated with CakePHP Framework"

php - 使用 PHP 从 MySQL 文件创建 zip 存档

mysql - 如何仅在一个查询中的计数中包含零

MySQL 规范化数据并不总是存在

asp.net-mvc - 用户个人资料图片 asp.net mvc 身份