2010-08-16 148 views
0

我一直在尝试使用php显示数据库中的图像,但是我只有在我使用或单击链接时才会下载提示。我想要做的是,我想在Web浏览器上显示它。并希望在标签中使用它。如何显示使用PHP存储在mysql数据库中的图像

我的代码是:

require_once('dbconfig.php'); 
     $cont=mysql_connect($dbhost,$dbuser,$dbpass) or die("Error Connecting the Database"); 
     $seldatabase=mysql_select_db($dbselect,$cont); 
     $insert = "SELECT `p_mime`, `p_name`, `p_size`, `p_data` FROM $dbtbl_reg_details WHERE `id` = $id"; 
     $query = mysql_query($insert); 
     if ($query) 
     { 
      if (mysql_num_rows($query)==1) 
      { 
       $row = mysql_fetch_assoc($query); 
       header("Content-Type: image/png"); 
       header("Content-Length: ". $row['p_size']); 
       header("Content-Disposition: inline; filename=". $row['p_name']); 
       echo $row[p_data]; 
      } 
     else 
     { 
      echo "image with id = ".$id." does not exist"; 
     } 
    } 
    else 
    { 
     echo "query failed, image with id = ".$id." does not exist"; 
    } 

当我使用内联的内容处置:然后我的网页返回浏览器上的脚本错误。

所以,我应该做些什么来显示网页上的图片,同时从mysql数据库

+3

为什么你要存储在数据库中的图像? – 2010-08-16 08:12:57

+0

相关:http://stackoverflow.com/questions/3748/storing-images-in-db-yea-or-nay – 2010-08-16 08:31:38

回答

1
if (mysql_num_rows($query)==1) 
{ 
    while(@ob_end_clean()); 

    header("Content-type: image/png"); 
    header("Content-Length: ". $row['p_size']); 
    header("Content-Disposition: attachment; filename=\"{$row['p_name']}\""); 

    die($row[p_data]); 
} 
+0

嗨!这个''。$ row ['p_name']。'“'正在给我返回一个语法错误 – 2010-08-16 10:50:56

+0

我已经编辑过这个语句,请检查一下。 – 2010-08-16 10:55:37

+0

while(@ob_end_clean);是诀窍..它帮助:) – 2010-08-16 10:58:51

0

检索它为什么ü有echo hi;

它可能会导致错误

,并在 年底增加出口(以确保你不要回应任何后)

echo $row[p_data]; 
exit; 
+0

您好,我已经删除了回声您好,并作出内容处置:内联..现在得到一个错误“图像”http://127.0.0.1/isc2011/photo.php?id=2“无法显示,因为它包含错误。” 我已经正确上传了文件,可能是什么原因!? – 2010-08-16 10:55:23

相关问题