2013-08-01 101 views
0

我正在學習php和mysqli。我編寫了一個程序,允許用戶將名稱和圖像上傳到數據庫。他們可以點擊數據庫中的名字並點擊名字,他們會看到該人的照片。圖像如何顯示?

下面是我的PHP文件。除了圖像不顯示之外,一切都可以工作。當我點擊一個名字時,我看到的只是一個帶有問號的藍色框。有人可以看看我的代碼,並給我一些關於如何解決這個問題的提示嗎?謝謝!

main.php

<?php 

    //Turn on error reporting 
    ini_set('display_errors', 'On'); 
    //Connects to the database 
    $mysqli = new mysqli("abc", "abc","abc", "abc"); 
    if($mysqli->connect_errno){ 
     echo "Connection error: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; 
    } 
?> 


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html> 
    <head> 
     <script src="http://code.jquery.com/jquery-1.10.2.min.js"></script> 
     <script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.11.1/jquery.validate.js"></script> 
     <script>$(document).ready(function() {$("form").validate();});</script> 
     <title>Homework</title> 
     <meta charset="UTF-8"> 
     <p></p> 
    </head> 
    <body> 
     <div>Homework</div> 
     <p></p> 
     <div> 
      <form method="post" action="index.php" enctype="multipart/form-data"> 
       Celebrity Name: <input type="text" name="c_name"> 
       Celebrity Photo: <input type="file" name="c_picture"> 
       <input type="submit" name="add" value="Upload"> 
      </form> 
     </div> 
     <br> 
     <div> 
     <table> 
     <tr> 
      <td>See below for a list of celebrities in our database</td> 
     </tr> 
     <?php 
      //Display names in the celebrity database 
      if(!($stmt = $mysqli->prepare("SELECT c_id, c_name FROM celebrity"))){ 
       echo "Prepare failed: " . $stmt->errno . " " . $stmt->error; 
      } 

      if(!$stmt->execute()){ 
       echo "Execute failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error; 
      } 

      if(!$stmt->bind_result($c_id, $c_name)){ 
       echo "Bind failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error; 
      } 

      while($stmt->fetch()){ 
       echo "<tr><td><a href='img.php?c_id=".$c_id."'>" . $c_name . "</a></td></tr>"; 
       //echo "<tr>\n<td>\n" . $c_name . "\n</td>\n</tr>"; 

      } 

      $stmt->close(); 
     ?> 
     </table> 
     </div> 
    </body> 
</html> 

img.php

<?php 

    //Turn on error reporting 
    ini_set('display_errors', 'On'); 
    //Connects to the database 
    $mysqli = new mysqli("blah", "blah","blah", "blah"); 
    if($mysqli->connect_errno){ 
     echo "Connection error: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; 
    } 

    $c_id=1; 
    //retrieve the blob 
    if(!($stmt = $mysqli->prepare("SELECT c_picture FROM celebrity where c_name = ?"))){ 
     echo "Prepare failed: " . $stmt->errno . " " . $stmt->error; 
    } 

    //if(!($stmt->bind_param("i",$_GET['c_id']))){ 
    if(!($stmt->bind_param("i",$c_id))){ 
     echo "Bind failed: " . $stmt->errno . " " . $stmt->error; 
    } 

    if(!$stmt->execute()){ 
     echo "Execute failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error; 
    } 

    $stmt->store_result(); 

    if(!$stmt->bind_result($c_picture)){ 
     echo "Bind failed: " . $mysqli->connect_errno . " " . $mysqli->connect_error; 
    } 

    $stmt->fetch(); 
    header("Content-Type: image/jpeg"); 
    echo $c_picture; 

    $stmt->close(); 
?> 

的index.php

<?php 

    //Turn on error reporting 
    ini_set('display_errors', 'On'); 
    //Connects to the database 
    $mysqli = new mysqli("blah", "blah","blah", "blah"); 
    if($mysqli->connect_errno){ 
     echo "Connection error: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; 
    } 

    if($_SERVER['REQUEST_METHOD'] == 'POST') 
    { 
     $errorinfo = $_FILES['c_picture']['error']; 
     $img = $_FILES['c_picture']['name']; 
     $imgTmp = $_FILES['c_picture']['tmp_name']; 
     $imgSize = $_FILES['c_picture']['size']; 
     $imgType = $_FILES['c_picture']['type']; 
    } 

    //Check image type and size 
    if (($imgType == 'image/jpeg' || $imgType == 'image/gif' || $imgType == 'image/png') && $imgSize < 1048576) 
    { 
     //Insert name and image file into celebrity database (c_name, c_picture) values (?, ?) 
     if(!($stmt = $mysqli->prepare("INSERT INTO celebrity(c_name, c_picture) VALUES (?, ?)"))) 
     { 
      echo "Prepare failed: " . $stmt->errno . " " . $stmt->error; 
     } 


     if(!($stmt->bind_param("sb",$_POST['c_name'], $img))) 
     { 
      echo "Bind failed: " . $stmt->errno . " " . $stmt->error; 
     } 


     if(!$stmt->execute()) 
     { 
      echo "Execute failed: " . $stmt->errno . " " . $stmt->error; 
     } 
     else 
     { 
      echo "Added " . $stmt->affected_rows . " celebrity to database."; 
     } 

    } 
    else 
    { 
     echo "Only jpegs, gifs, and pngs under 1MB can be uploaded"; 
    } 
?> 

回答

2

插入時,你是不是在保存的圖像數據,如果打印:

$img = $_FILES['c_picture']['name']; 
echo $img; 

只有圖像的原始名稱,而不是你需要保存的文件數據。如果你想只輸出圖像直接:

header("Content-Type: image/jpeg"); 
echo file_get_contents($_FILES['c_picture']['tmp_name']); 

你需要做的是從$_FILES['c_picture']['tmp_name']採取圖像數據,並存儲在數據庫中的BLOB或LONGBLOB。我看你已經在使用BLOB,所以你只需要改變:

$img = $_FILES['c_picture']['name']; 

而是使用:

$img = file_get_contents($_FILES['c_picture']['tmp_name']); 

而且,你的頭應該輸出正確的圖像類型,也許你可以將它保存到數據庫也是如此。

在現實世界的應用程序中,您不應該將圖像的二進制文件存儲在數據庫中,更好的方法是將圖像存儲在文件夾中,然後將地址保存到數據庫中。

0

到您的main.php顯示圖片的頁面的鏈接是img.php

<a href='img.php?c_id=".$c_id."'> 

然而,你張貼在這裏的顯示畫面viewfile.php文件的名稱,所以只要改變你的鏈接指向viewfile.php:

<a href='viewfile.php?c_id=".$c_id."'> 
+0

對不起。我將編輯我的帖子。它應該是「img.php」。它仍然不起作用。 – user2203774

0

先確認一下,如果你輸入了完整的圖像在db標籤,然後還有一個問題,但如果你只是輸入圖像的名稱數據庫,以查看它,你應該做這樣的事情

<img src="images/$c_name" alt="$c_name"> 

而不是僅僅$ c_name的。