php数据库图片显示问题 [英] php database image show problem

查看:40
本文介绍了php数据库图片显示问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是代码

<头><身体><中心><div id="ser"><form action="" method="post"><label for="file">卡号:</label><input type="text" name="card_no" id="card_no" class="fil" onKeyUp="CardNoLength()" onKeyDown="CardNoLength()" onKeyPress="CardNoLength()"/><input type="submit" name="search" value="Search" class="btn" onClick="return CardNoLengthMIN()"/></表单>

</中心><br/><hr style="border: 1px solid #606060 ;"/><center><a href="index.php">首页</a></center><br/><中心><?phpif(isset($_POST['card_no'])){如果($conn){if(mysql_select_db("img_mgmt", $conn)){$sql = "select * from temp_images where card_no='".trim($_POST['card_no'])."'";$result = mysql_query($sql);$image = mysql_fetch_array($result);if(isset($image['card_no'])){//echo "<img src=\"".$image['file_path']."\" alt=\"".$image['card_no']."\" width=\"250\" height=\"280\"/>";header("内容类型:图片/jpeg");回声 $image['img_content'];}别的{echo "<p style=\"color:red;\">抱歉,您的搜索没有结果!<br/>尝试使用不同的卡号";}}别的{echo "数据库选择错误:".mysql_error();}}别的{echo "无法连接:".mysql_error();}}?></中心></html>

但它在执行脚本后显示:

<块引用>

无法修改头信息 -已发送的标头(输出开始于C:\xampp\htdocs\img\search.php:61) 中C:\xampp\htdocs\img\search.php 上线77

解决方案

如果您已将图像的内容保存在数据库中,您可以使用 data: uri 将这些内容显示在 <代码> 标签.这允许您将不同 mime 类型的内容嵌入到另一个内容中,请参阅数据 URI 方案.

但是你真的不应该在数据库中保存任何文件(无论图像与否).文件属于文件系统,顾名思义.在数据库中保存文件时会产生巨大的开销.特别是对于您需要(可能)加载图像的 php 脚本的图像,例如.如果你使用类似 <img src="showimage.php?id=5" alt="..."/> 的东西.对于每个图像,您都需要调用一个额外的 php 脚本,但您一无所获.每个人都会告诉你最好将文件保存在文件系统中像往常一样通过文件系统加载它.所以你使用像 <img src="images/foobar/xyz.png" alt="..."/> 这样的标签.即使 我不希望我的数据库中的图像有任何‘断开的链接’" 参数也不算在内,因为您只需使用路径中的 ID 并使用 file_exists()检查图片链接是否有效.

$path = 'images/useravatars/'.$row['ID'].'.png';//举个例子如果(文件存在($路径)){echo '<img src="'.$path.'" alt="用户名"/>';} 别的 {echo '<img src="images/noimage.png" alt="未找到图像"/>';}

here is the code

<?php
 session_start();

 if(!isset($_SESSION['user_name']))
 {
  header('Location: login.php');
 }

 $conn = mysql_connect("localhost", "root", "") or die("Can no connect to Database Server");
?>

<html>
<head>

</head>
<body>
<center>
<div id="ser">
<form action="" method="post">


<label for="file">Card No:</label>
<input type="text" name="card_no" id="card_no" class="fil" onKeyUp="CardNoLength()" onKeyDown="CardNoLength()" onKeyPress="CardNoLength()"/>
<input type="submit" name="search" value="Search" class="btn" onClick="return CardNoLengthMIN()"/>
</form>
</div>
</center>
<br/><hr style="border: 1px solid #606060 ;" />
<center><a href="index.php">Home</a></center>
<br/>

<center>
<?php

 if(isset($_POST['card_no']))
 {
  if($conn)
  {
   if(mysql_select_db("img_mgmt", $conn))
   {
    $sql = "select * from temp_images where card_no='".trim($_POST['card_no'])."'";
    $result = mysql_query($sql);
    $image = mysql_fetch_array($result);

    if(isset($image['card_no']))
    {

      //echo "<img src=\"".$image['file_path']."\" alt=\"".$image['card_no']."\" width=\"250\" height=\"280\"/>";
      header("Content-type: image/jpeg");
      echo $image['img_content'];

    }
    else
    {
      echo "<p style=\"color:red;\">Sorry, Your search came with no results ! <br/> Try with different card number";
    }
   }
   else
   {
    echo "Database selection error: ".mysql_error();
   }
  }
  else
  {
   echo "Could not connect: ".mysql_error();
  }
 }
?>
</center>

</body>
</html>

But it after executing the script it shows:

Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\img\search.php:61) in C:\xampp\htdocs\img\search.php on line 77

解决方案

If you have save the content of the image in the database you can use a data: uri do show this content in a <img /> tag. This allows you to embed content of different mime type in another content, see data URI scheme.

But you shouldn't really save any files (regardless of image or not) in the database. Files belongs to the filesystem, as the name already suggest. You get a huge overhead in saving files in the database. Specially for images you need (maybe) a php script which loads the image, eg. if you use something like <img src="showimage.php?id=5" alt="..." />. For each image you need to call an additional php script, and you gain nothing. Everyone will tell you its better to save the files in the filesystem and load it via the filesystem as normal. So you use tags like <img src="images/foobar/xyz.png" alt="..." /> instead. Even the "I don't want any 'broken links' to images in my database" argument doesn't count as you simply use the ID inside the path and use file_exists() to check if an image link is valid or not.

$path = 'images/useravatars/'.$row['ID'].'.png'; // as an example
if (file_exist($path)) {
     echo '<img src="'.$path.'" alt="username" />';
} else {
     echo '<img src="images/noimage.png" alt="No Image found" />';
}

这篇关于php数据库图片显示问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆