【问题标题】:Upload image in MySQL database在 MySQL 数据库中上传图像
【发布时间】:2016-06-23 20:34:50
【问题描述】:

我正在使用此脚本尝试将图像与表单一起上传到我的数据库中。问题是当我在查询中包含$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!";
        }
    }
}

?>

【问题讨论】:

  • 将文件保存到磁盘并将文件路径,其他信息...保存到mysql!
  • 回显insert查询,可能有一些内容'$cover`破坏了查询。要跳过它,请使用 addlashes() 或此类功能

标签: php mysql image-uploading


【解决方案1】:

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

  $cover = base64_encode($cover);

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

【讨论】:

    【解决方案2】:

    您不应将图像直接上传到您的数据库中。因为这会使你的数据库变大,你的反应会很慢。您可以将图像上传到单独的目录并将目录存储到数据库中。这是归档目标的有效方式。 顺便说一句,您可以这样做,但您应该选择二进制字段。该字段将存储二进制数据。

    【讨论】:

      【解决方案3】:

      建议您使用 PDO 和 PDO::PARAM_LOB,而不是使用 mysqli 扩展名 - 可以在此处找到一个简单的示例:http://php.net/manual/en/pdo.lobs.php#example-1021

      【讨论】:

        【解决方案4】:

        如果你真的想将图片直接存储在mysql db中,那么这篇文章应该对你有所帮助:How to upload images into MySQL database using PHP code

        但一般来说,将图像上传到您的 ftp 服务器会更容易,可能会创建一个名为 images 或 uploads 的文件夹,然后将文件名存储在 sql db 中。然后,当您获取文件名时,您可以从图像文件夹中加载正确的图像。

        这是另一篇可能对您有所帮助的帖子:Upload image to server and store image path in mysql database

        基本上你这样做:

        $file_path = "uploads/";
        
        $file_path = $file_path . basename( $_FILES['uploaded_file']['name']);
        if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) {
             // replace $host,$username,$password,$dbname with real info
             $link=mysqli_connect($host,$username,$password,$dbname);
             mysqli_query($link,"INSERT INTO `files` (filename,path) VALUES ('".$_FILES['uploaded_file']['tmp_name']."','".$file_path."')") or trigger_error($link->error."[ $sql]");
             mysqli_close($link);
        }
        

        【讨论】:

        • 这是一种常用的方法,但是可以直接将图片存储在db中。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-23
        • 1970-01-01
        • 2019-07-10
        • 2019-11-03
        • 2017-05-14
        相关资源
        最近更新 更多