显示图像 - blob - 不正确

时间:2011-10-05 12:35:25

标签: php blob

在某个地方我会非常错误。我正在尝试在php中显示来自MySQL表的图像。我将从头开始。

uploadform

<!DOCTYPE html>
    <head>
      <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <form action="add_file.php" method="post" enctype="multipart/form-data">
            <input type="file" name="uploaded_file"><br />
            <input type="submit" value="Upload file">
        </form>
        <p>
            <a href="list_files.php">See all files</a>
        </p>
    </body>
    </html>

add_file.php

<?php
// Check if a file has been uploaded
if(isset($_FILES['uploaded_file'])) {
    // Make sure the file was sent without errors
    if($_FILES['uploaded_file']['error'] == 0) {
        // Connect to the database
        $dbLink = new mysqli('localhost', 'root', '', 'mydb');
        if(mysqli_connect_errno()) {
            die("MySQL connection failed: ". mysqli_connect_error());
        }

        // Gather all required data
        $name = $dbLink->real_escape_string($_FILES['uploaded_file']['name']);
        $mime = $dbLink->real_escape_string($_FILES['uploaded_file']['type']);
        $data = $dbLink->real_escape_string(file_get_contents($_FILES  ['uploaded_file']['tmp_name']));
        $size = intval($_FILES['uploaded_file']['size']);

        // Create the SQL query
        $query = "
            INSERT INTO `file` (
                `name`, `mime`, `size`, `data`, `created`
            )
            VALUES (
                '{$name}', '{$mime}', {$size}, '{$data}', NOW()
            )";

        // Execute the query
        $result = $dbLink->query($query);

        // Check if it was successfull
        if($result) {
            echo 'Success! Your file was successfully added!';
        }
        else {
            echo 'Error! Failed to insert the file'
               . "<pre>{$dbLink->error}</pre>";
        }
    }
    else {
        echo 'An error accured while the file was being uploaded. '
           . 'Error code: '. intval($_FILES['uploaded_file']['error']);
    }

    // Close the mysql connection
    $dbLink->close();
}
else {
    echo 'Error! A file was not sent!';
}
?>

显示图片

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("mydb", $con);
$id = 1;
if(!isset($id) || empty($id) || !is_int($id)){
     die("Please select your image!");
}else{  
$query = mysql_query("SELECT * FROM file WHERE id='".$id."'");
$row = mysql_fetch_array($query);
$content = $row['data'];  
header('Content-type: image/jpg');
     echo $content;
}  
?>

我的输出是这样的: JFIFHH C

2 个答案:

答案 0 :(得分:0)

您需要使用imagecreatefromstringimagejpeg

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
    die('Could not connect: ' . mysql_error());
}

mysql_select_db("mydb", $con);
$id = 1;
if(!isset($id) || empty($id) || !is_int($id)){
     die("Please select your image!");
} else {  
    $query = mysql_query("SELECT * FROM file WHERE id='".$id."'");
    $row = mysql_fetch_array($query);

    $im = imagecreatefromstring($row['data']);

    if ($im !== false) {
        header('Content-Type: image/jpeg');
        imagejpeg($im);
        imagedestroy($im);
    }
}  
?>

答案 1 :(得分:0)

在您需要编码数据库blob的图像数据中尝试此操作

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