2011-10-05 122 views
2

某处我要犯错。我试图从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; 
} 
?> 

我的输出是是这样的:JFIFHHC

+0

什么是表格类型?它是否设置为“BLOB”? – Mob

+0

id = int,data = mediumblob – Wilest

+0

@ genesis-φ为什么我不应该将图像存储到数据库中? – Wilest

回答

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); 
    } 
} 
?> 
0

试试这个在您需要从数据库编码的团块中的图像数据

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