在MySQL数据库中上传图片

时间:2016-06-23 20:34:50

标签: 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!";
        }
    }
}

?>

4 个答案:

答案 0 :(得分:0)

如果你真的想直接将图像存储在mysql数据库中,那么这篇文章可以帮助你: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);
}

答案 1 :(得分:0)

在加载后将文件转换为字符格式;

  $cover = base64_encode($cover);
在插入之前

。您还应该在sql语句中使用绑定。

答案 2 :(得分:0)

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

答案 3 :(得分:0)

我建议你使用PDO和PDO :: PARAM_LOB,而不是使用<fragment xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/fragment" android:name="com.example.android.sunshine.app.ForecastFragment" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:layout="@layout/fragment_main" /> 扩展名 - 可以在这里找到一个简单的例子:http://php.net/manual/en/pdo.lobs.php#example-1021