dqjmq28248 2014-01-18 15:06
浏览 46
已采纳

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

I am trying to store images in mySQL database and then displaying on the other page. I have this function to store images in 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;
    }

And this code to show 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);
    ?>

When this code is run, nothing is shown on the page, even if I write some HTML inside this code, empty page is shown.

  • 写回答

2条回答 默认 最新

  • douchaqi3369 2014-01-18 15:20
    关注

    I assume you are saving the image content as blob and if your sql is returning the correct data then you can display as

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

    You need to add the header first before the image.

    or

    echo '<img src="data:image/jpeg;base64,' . base64_encode($row['carPicture']) . '">';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目