2013-05-30 63 views
0

我需要顯示一個BLOB數據。 list_files.php顯示BLOB數據

<?php 
// Connect to the database 
$dbLink = new mysqli('127.0.0.1', 'root', '', 'db_name'); 
if(mysqli_connect_errno()) { 
    die("MySQL connection failed: ". mysqli_connect_error()); 
} 

// Query for a list of all existing files 
$sql = 'SELECT `id`, `name`, `mime`, `size`, `created` FROM `file`'; 
$result = $dbLink->query($sql); 

// Check if it was successfull 
if($result) { 
    // Make sure there are some files in there 
    if($result->num_rows == 0) { 
     echo '<p>There are no files in the database</p>'; 
    } 
    else { 
     // Print the top of a table 
     echo '<table width="100%"> 
       <tr> 
        <td><b>Name</b></td> 
        <td><b>Mime</b></td> 
        <td><b>Size (bytes)</b></td> 
        <td><b>Created</b></td> 
        <td><b>&nbsp;</b></td> 
       </tr>'; 

     // Print each file 
     while($row = $result->fetch_assoc()) { 
      echo " 
       <tr> 
        <td>{$row['name']}</td> 
        <td>{$row['mime']}</td> 
        <td>{$row['size']}</td> 
        <td>{$row['created']}</td> 
        <td><a href='get_file.php?id={$row['id']}'>Download</a></td> 
       </tr>"; 
     } 

     // Close table 
     echo '</table>'; 
    } 

    // Free the result 
    $result->free(); 
} 
else 
{ 
    echo 'Error! SQL query failed:'; 
    echo "<pre>{$dbLink->error}</pre>"; 
} 

// Close the mysql connection 
$dbLink->close(); 
?> 

這是我的代碼來顯示數據。 輸出

Name   Mime    Size (bytes)    Created 
cubeatwork.jpg image/jpeg 38717 2013-05-29 16:45:17 Download 
9cube.jpg image/jpeg 23187 2013-05-30 10:08:37 Download 

但我需要diaplay,而不是這個數據圖像。 該怎麼做。 請幫我

回答

0

您需要指定一個標題,然後將其打印爲圖像。 就像下面的代碼在你的循環中。

header('Content-Type: image/jpeg');

OR

header('Content-Type: '.$row['mime']);

echo $row['size']; // if 'size' is a blob type

希望這將幫助你

+0

header('Content-Type:image/jpeg');我已將此列入頁面頂部。但是,現在該頁面是空白的。 – Ammu

+0

while($ row = $ result-> fetch_assoc()){ \t \t \t header('Content-Type:'。$ row ['mime']); 回聲「 ​​{$行[ '名稱']} ​​{$行[ 'MIME']} ​​{$行[ '尺寸']} ​​{$行[ '創造' ]} \t \t \t \t \t頭( '內容 - 類型:圖像/ JPEG'); ​​Download 「; } --------------------無法修改標題信息 - 已發送的標題 – Ammu

0
// Print each file 
    while($row = $result->fetch_assoc()) {  
echo '<img alt="image" src="data:image/jpeg;base64,' . chunk_split(base64_encode($row['size'])) . '">'; 
    } 
OR 
header('Content-Type: image/jpeg');// 
while($row = $result->fetch_assoc()) { 
    echo $row["size"]; 

}