Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 从MySql检索并显示图像_Php_Mysql_Image - Fatal编程技术网

Php 从MySql检索并显示图像

Php 从MySql检索并显示图像,php,mysql,image,Php,Mysql,Image,我已经成功地在mySql中存储了一个图像。我想检索输入输出,并在网站上显示它与a href按钮,能够扩大它 这是我的数据库表结构 -- Table structure for table `poster` -- CREATE TABLE `image` ( `img_id` int(11) NOT NULL auto_increment, `img_name` varchar(32) NOT NULL, `img` blob NOT NULL, `img_type` varch

我已经成功地在mySql中存储了一个图像。我想检索输入输出,并在网站上显示它与a href按钮,能够扩大它

这是我的数据库表结构

-- Table structure for table `poster`
--

CREATE TABLE `image` (
  `img_id` int(11) NOT NULL auto_increment,
  `img_name` varchar(32) NOT NULL,
  `img` blob NOT NULL,
  `img_type` varchar(32) NOT NULL,
  `img_size` int(255) NOT NULL,
  PRIMARY KEY  (`img_id`)
)
显示图像的位置

<div class="col-md-5 text-dimension">
        <?php
            $con=mysql_connect("localhost","root","");
            mysql_select_db("isiti");

            if (mysqli_connect_errno($con))
             {
                echo "Failed to connect to MySQL: " . mysqli_connect_error();
             }

            $result = mysql_query("SELECT * FROM poster ORDER BY year DESC LIMIT 0, 5000");
                while($row = mysql_fetch_array($result))
                {
                    $id=$row['img_id'];
                    echo '<a href="img_disp.php?image_id='.$id.'" data-lightbox="image-1" title="©ISITI-CoERI"><center><img src="img_disp.php?image_id='.$id.'" alt="poster1" width="200" height="250"><br></a><br><br>';
                    echo '<span class=right><a href="edit_form.php?id='.$id.'">[edit]</a>
                    <a name="delete" id="delete" href="public_delete.php?id='.$id.'">[delete]</a></span>';
                }
        ?>

这是img_disp.php

<?php

   // Connect to the database
        $host="localhost"; // Host name 
        $username="root"; // Mysql username 
        $password=""; // Mysql password 
        $db_name="isiti"; // Database name 
        $tbl_name="poster"; // Table name 

        $conn = mysql_connect("$host", "$username", "$password"); 
        if(! $conn )
        {
          die('Could not connect: ' . mysql_error());
        }
        if (isset($_GET["image_id"]) && !empty($_GET["image_id"])) 
            {                           
                $id =$_GET["image_id"];
            }

        mysql_select_db($db_name);

  $sql="SELECT * from poster where id='$id'";

  $query=mysql_query($sql) or die(mysql_error());

  while($result=mysql_fetch_array($query)){     
    header('Content-type:'.$result['img_type'].'');
    echo $result['img'];
    header('Content-Disposition: inline; filename="'.$result['img_name'].'"');      
  }
?>


问题是。。。图像没有显示出来。为两者或弹出

我认为问题在于您使用的是echo,它用于处理字符串。此外,您可能需要考虑其他方法来存储图像,而不是使用MySQL。无论如何,正确的方法是打开一个文件流,然后将数据放入它们的文件中。此外,您还需要一个单独的文件来处理图片,因为您只能更改整个文件的标题。这意味着您需要一个文件来创建图片的显示页面,另一个文件来调出图片

$write=fopen("php://output","w");
fwrite($write,$results['img']);
fclose($write);

另外,我不确定您是否具有适合mysql结果的结构。

我建议将所有图像存储在磁盘[文件系统]中,而不是mysql中。 您还可以加密图像并将其存储在.dat文件中。这将确保图像的安全性

在MySQL中,只需存储特定图像的路径。当您从MySQL检索记录时

go to the image path
get image
decrypt it (Optional, Only if you have encrypted it before)
and display the image..

回音后不能再执行任何header()操作。必须先发送所有标题,然后才发送数据。如何将图像存储到文件系统中?有什么例子吗?如果您允许用户上传图像,那么您可以使用PHP move_upload_file将上传的图像/文件保存到您的文件夹中。