无法使用PHP显示存储在mySQL中的图像

时间:2014-01-18 15:06:34

标签: php html blob wampserver

我正在尝试将图像存储在mySQL数据库中,然后在另一页上显示。我有这个函数在mySQL中存储图像。

 function upload() {
        include "databaseConnection.php";
        $maxsize = 10000000; //set to approx 10 MB
        //check associated error code
        if ($_FILES['userfile']['error'] == UPLOAD_ERR_OK) {

            //check whether file is uploaded with HTTP POST
            if (is_uploaded_file($_FILES['userfile']['tmp_name'])) {

                //checks size of uploaded image on server side
                if ($_FILES['userfile']['size'] < $maxsize) {


                    // prepare the image for insertion
                    $imgData = addslashes(file_get_contents($_FILES['userfile']['tmp_name']));

                    // put the image in the db...
                    // database connection
                    mysql_connect($host, $user, $pass) OR DIE(mysql_error());

                    // select the db
                    mysql_select_db($db) OR DIE("Unable to select db" . mysql_error());

                    // our sql query
                    $sql = "INSERT INTO carsinfo
                (carName, carPicture)
                VALUES
                ('{$_FILES['userfile']['name']}', '{$imgData}');";

                    // insert the image
                    mysql_query($sql) or die("Error in Query: " . mysql_error());
                    $msg = '<p>Image successfully saved in database with id =' . mysql_insert_id() . ' </p>';
                } else {
                    // if the file is not less than the maximum allowed, print an error
                    $msg = '<div>File exceeds the Maximum File limit</div>
            <div>Maximum File limit is ' . $maxsize . ' bytes</div>
            <div>File ' . $_FILES['userfile']['name'] . ' is ' . $_FILES['userfile']['size'] .
                            ' bytes</div><hr />';
                }
            }
            else
                $msg = "File not uploaded successfully.";
        }
        else {
            $msg = file_upload_error_message($_FILES['userfile']['error']);
        }
        return $msg;
    }

此代码显示iamges。

    <?php
    include "databaseConnection.php";
    // just so we know it is broken
    error_reporting(E_ALL);
    // some basic sanity checks
    //connect to the db
    $link = mysql_connect("$host", "$user", "$pass") or die("Could not connect: " . mysql_error());

    // select our database
    mysql_select_db("$db") or die(mysql_error());
    // get the image from the db
    $sql = "SELECT carPicture FROM carsinfo;";
    // the result of the query
    $result = mysql_query("$sql") or die("Invalid query: " . mysql_error());
    $row = mysql_fetch_assoc($result);
    // set the header for the image
    echo $row['carPicture'];
    header("Content-type: image/jpeg");
    // close the db link
    mysql_close($link);
    ?>

运行此代码时,页面上不显示任何内容,即使我在此代码中编写了一些HTML,也会显示空白页。

2 个答案:

答案 0 :(得分:2)

我假设您将图像内容保存为blob,如果您的sql正在返回正确的数据,那么您可以显示为

header("Content-type: image/jpeg");
echo $row['carPicture'];

您需要先在图像前添加标题。

echo '<img src="data:image/jpeg;base64,' . base64_encode($row['carPicture']) . '">';

答案 1 :(得分:0)

不要写 包括databaseConnection.php

没有任何内容。