2015-04-07 171 views
-1

我想用PHP顯示來自MySQL的圖片。我寫了代碼,但向我顯示的錯誤消息是 ((圖像「http://localhost/abshaaz/ViewImage.php」無法顯示,因爲它包含錯誤)),並且背景顏色變成黑色。如果我使用If()函數,它將顯示程序的else部分,這是一條錯誤消息:「加載圖像類型錯誤!」 這裏是代碼,請有人幫我找出錯誤: 但在此之前,我親自懷疑header()函數,因爲當它被移除時它將顯示一個非格式或非類型的圖像(龐大的不可讀代碼) ,但每當放回上述錯誤發生。如何使用PHP顯示存儲在MySQL中的圖像?

  • 數據庫名稱:abshaaz
  • 表名:畫冊
  • 列名:圖像標識,ImageName,鏡像文件

`

<?php 

mysql_connect("127.0.0.1", "root",""); 
mysql_select_db("abshaaz"); 


if(isset($_GET['ImageID'])) 
{ 

    $imageid=mysql_real_escape_string($_GET['ImageID']); 
    $query=mysql_query("SELECT * FROM photoalbum WHERE ImageID='$imageid'"); 

    while($row=mysql_fetch_assoc($query)) 

    { 
     $imageData= $row["ImageFile"]; 

    } 
     header("Content-type: image/jpeg"); 

     echo $imageData; 
} 
else 
{ 
    echo "loading image type error!"; 
} 

?>` 

然後我用圖像標籤下方的另一個頁面調用圖像

<img src="ViewImage.php?ImageID=5" width=100 height=100> 
+0

你把圖片上傳到任意文件夾?你必須給不PHP文件 – user3386779

+0

圖像文件的路徑,你居然stori ng數據庫中圖像的內容?或者ImageFile只是對某個名字的引用,比如something.jpg – MrTechie

+0

似乎值得一提的是,您正在使用不推薦使用的庫,如[PHP手冊中所述](http://php.net/manual/en/intro.mysql .PHP);是否有一個原因,你沒有(至少)使用[MySQLi](http://php.net/manual/en/book.mysqli.php)版本? –

回答

0

最佳做法來存儲照片是在文件系統本身。您應該將照片保存在一個文件夾中,並將文件名保存到數據庫中。例如: 例如: 如果在images文件夾中通過image.jpg名稱保存圖像,則必須將image.jpg的名稱保存在images文件夾中。

<?php 

mysql_connect("127.0.0.1", "root",""); 
mysql_select_db("abshaaz"); 


if(isset($_GET['ImageID'])) 
{ 

    $imageid=mysql_real_escape_string($_GET['ImageID']); 
    $query=mysql_query("SELECT * FROM photoalbum WHERE ImageID='$imageid'"); 

$imageData = array(); 

    while($row=mysql_fetch_assoc($query)) 

    { 
     $imageData[] = $row["ImageFile"]; 

    } 
     //header("Content-type: image/jpeg"); 

     //echo $imageData; 
foreach($imageData as $image) { 
    echo "<img src=\"$image['ImageFile']\" />"; 
} 
} 
else 
{ 
    echo "loading image type error!"; 
} 

?>` 
+0

謝謝@Tony Burton先生爲此做出的巨大貢獻。請如果你能幫我把代碼存儲在文件夾中的圖像和表中的圖像名稱。因爲我只知道你看到的方法。謝謝。 –

0

我一直在使用的mysqli替代MySQL的書面這個答案,這答案將圖像返回到瀏覽器的請求或返回,如果遇到錯誤不赫德找到了404。它還會將錯誤記錄到文件中供管理員查看。

此代碼將根據圖像ID向服務器上的映像查詢絕對路徑和文件名的數據庫。它還會驗證輸入並在執行數據庫查詢之前對其進行過濾。

對於成功的查詢,它將在讀取文件內容並使用jpeg標題回顯給瀏覽器之前,檢查服務器上是否存在該文件。

<?php 

include 'database_settings.php'; 

$mysqli = @new mysqli ($mysql_hostname, $mysql_username, 
    $mysql_password, $mysql_database); 

if ($mysqli->connect_errno > 0) { 
    header ($_SERVER [ 'SERVER_PROTOCOL' ] . ' 404 Not Found'); 
    file_put_contents ('path/to/error.log', 'There was an error connecting to the database in file ' . 
     __FILE__ . PHP_EOL, FILE_APPEND); 

    return; // quit execution of the file 
} 

if ((isset ($_GET [ 'ImageID' ])) && (ctype_digit ($_GET [ 'ImageID' ]))) { 

    $query = $mysqli->query (sprintf ("SELECT * FROM photoalbum WHERE ImageID=%d", 
     $mysqli->real_escape_string ($_GET [ 'ImageID' ]))); 

    if ($query->num_rows > 0) { 
     $filename = null; 

     while ($row = $query->fetch_assoc()) { 
      $filename = $row [ 'ImageName' ]; 
     } 

     if (isset ($filename)) { 
      if (file_exists ($filename)) { 
       header("Content-type: image/jpeg"); 
       print file_get_contents ($filename); 
      } else { 
       file_put_contents ('path/to/error.log', 'There was an locating the image ' . $filename . 
        ' on the server filesystem in file ' . 
        __FILE__ . ' on line ' . __LINE__ . PHP_EOL, FILE_APPEND); 
       header ($_SERVER [ 'SERVER_PROTOCOL' ] . ' 404 Not Found'); 
      } 
     } else { 
      file_put_contents ('path/to/error.log', 'There was an error getting an image filename from the database with an id of ' . 
       $_GET [ 'ImageID' ] . ' in file ' . __FILE__ . ' on line ' . __LINE__ . PHP_EOL, FILE_APPEND); 
      header ($_SERVER [ 'SERVER_PROTOCOL' ] . ' 404 Not Found'); 
     } 
    } else { 
     file_put_contents ('path/to/error.log', 'There was an error getting an image filename from the database with an id of ' . 
      $_GET [ 'ImageID' ] . ' in file ' . __FILE__ . ' on line ' . __LINE__ . PHP_EOL, FILE_APPEND); 
     header ($_SERVER [ 'SERVER_PROTOCOL' ] . ' 404 Not Found'); 
    } 

    $query->close(); // free the result from memory 
} else { 
    // no error needs to be logged 
    header ($_SERVER [ 'SERVER_PROTOCOL' ] . ' 404 Not Found'); 
} 

$mysqli->close(); // close the database connection 

// EOF 
+0

謝謝@邁克爾布什。我會嘗試使用這個你的代碼,現在。如果真的有效,我會讓你知道。謝謝。 –

+0

@salehzaharaddeen沒有問題,如果你需要進一步的幫助,請讓我知道。 –

+0

先生,我嘗試了所有可能的更改,包括連接代碼。我運行的代碼沒有錯誤,只是沒有顯示任何圖片。我甚至試圖在使用圖片標籤的另一個頁面上調用它,但仍然沒有顯示。我終於改變了從數據庫到文件夾基地的存儲位置,但仍然無法正常工作。正如我告訴你的,我不擅長這種方法,請不要因爲你的協助而感到厭倦。謝謝 –

0

我通常不喜歡在帖子中添加第二個答案,所以我很抱歉提前。 如果這個幫助,我會刪除我的其他答案。

文件名:ImageHandler.php

<?php 

class ImageHandler 
{ 
    public function __construct (mysqli $mysqli) { 
     $this->mysqli = $mysqli; 
    } 

    public function InsertImage ($filename) { 
     $absolute_filename = realpath ($filename); 
     if ($absolute_filename === true) { 
      if ($this->mysqli->query (sprintf ('INSERT INTO album (\'filename\') VALUES (\'%s\')', $absolute_filename))) { 
       return $this->mysqli->insert_id; 
      } 
      throw new Exception ('There was an error inserting the image into the database.'); 
     } 
     throw new Exception ('Image "' . $filename . '" could not be found on the server.'); 
    } 

    public function GetImage ($imageID) { 
     if ((is_integer ($imageID) == false) || ($imageID < 1)) { 
      throw new InvalidArgumentException ('The image ID must be an integer greater than 0'); 
     } 
     $query = $mysqli->query (sprintf ('SELECT * FROM album WHERE id=%d', $imageId)); 
     if ($query->num_rows > 0) { 
      $filename = null; 
      while ($row = $query->fetch_assoc()) { 
       $filename = $row [ 'filename' ]; 
      } 
      $query->close(); 
      if (isset ($filename)) { 
       $absolute_filename = realpath ($filename); 
       if ($absolute_filename === false) { 
        throw new Exception ('Image "' . $filename . '" could not be found on the server.'); 
       } 
       return $absolute_filename; 
      } 
      throw new Exception ('There was an error returning the image filename from the database.'); 
     } 
     $query->close(); 
     throw new Exception ('There was an error getting an image filename from the database with an image id of [' . $imageId . '].'); 
    } 

    public function PrintImage ($filename) { 
     $absolute_filename = realpath ($filename); 
     if ($absolute_filename === false) { 
      throw new Exception ('Image "' . $filename . '" could not be found on the server.'); 
     } 
     header ('Content-Type: image/jpeg'); 
     print file_get_contents ($absolute_filename); 
    } 

    private $mysqli; 
} 

?> 

示例用法打印圖像到瀏覽器。

在開始之前,我們將對服務器目錄結構做一些假設。 ImageHandler.php存儲在image.php旁邊,一個包含所有圖像的受保護圖像文件夾位於父目錄的一個目錄中,並且無法通過Web瀏覽器訪問,位於images文件夾旁邊的是一個名爲logs的文件夾。

image.php

<?php 

try { 
    if (isset ($_GET [ 'id' ]) == false) { 
     header ($_SERVER [ 'SERVER_PROTOCOL' ] . ' 404 Not Found'); 
     return; 
    } 
    include 'ImageHandler.php'; 
    include 'database_settings.php'; 
    $mysqli = @new mysqli ($mysql_hostname, $mysql_username, $mysql_password, $mysql_database); 
    if ($mysqli->connect_errno > 0) { 
     throw new Exception ('There was an error connecting to the database.'); 
    } 
    $image = new ImageHandler ($mysqli); 
    $filename = $image->GetImage ($_GET [ 'id' ]); 
    $image->PrintImage ($filename); 
} catch (Exception $e) { 
    header ($_SERVER [ 'SERVER_PROTOCOL' ] . ' 404 Not Found'); 
    file_put_contents ('../logs/error.log', $e->getMesage() . ' in file ' . $e->getFile() . ' on line ' . $e->getLine(). PHP_EOL, FILE_APPEND); 
} 

?> 

存儲信息到我建議把這個文件的密碼保護的登錄背後的數據庫,圖像需要已經位於圖像的文件夾裏面

saveimage。 PHP

<?php 

try { 
    if (isset ($_POST [ 'filename' ]) == false) { 
     throw new Exception ('Please complete this form to add an image to the database.'); 
    } 
    $path = realpath ('../images'); 
    if ($path === false) { 
     throw new Exception ('Images path does not exist.'); 
    } 
    $path .= DIRECTORY_SEPARATOR . $_POST [ 'filename' ]; 
    include 'ImageHandler.php'; 
    include 'database_settings.php'; 
    $mysqli = @new mysqli ($mysql_hostname, $mysql_username, $mysql_password, $mysql_database); 
    if ($mysqli->connect_errno > 0) { 
     throw new Exception ('There was an error connecting to the database.'); 
    } 
    $image = new ImageHandler ($mysqli); 
    $id = $image->InsertImage ($path); 
    print 'Image saved as ID ' . $id . '<br><br><img src="image.php?id=' . $id .'"/>'; 
} catch (Exception $e) { 
    // form would go here and $e->getMessage() would be used to display messages to the browser 
} 

?>