2011-10-05 5 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 
$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; 
} 
?> 

내 출력되는 화상을 표시

<?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!'; 
} 
?> 

뭔가 같이 : 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']) . '" />';