php 从mysql php获取图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20324194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:04:56  来源:igfitidea点击:

fetch image from mysql php

phpmysqlimage

提问by Karuppiah RK

here i am trying to upload the employee profile picture to mysql and then i want to fetch those picture. now i can uploaded the image into mysql db successfully but, when i try to fetch the image that doesn't fetch properly. if you open an image in texteditor na how it will look like? the same thing i got it now.. like this... (Q?f0±ó%"÷YúE?Vúo?e9°R}è"!5????\ˉ?????YU0T3?′?\aV?%zùBe??nPè Qx?e?o0′áPá”·=ó-)Oyá+×??S?T??S!Y¨;,?¤a5H?ó?íó3áo?j/"5±?RX?ee.&{ +C? H Cìai,F+?#”?ì8??IDO%IW).

在这里,我试图将员工个人资料图片上传到 mysql,然后我想获取这些图片。现在我可以成功地将图像上传到 mysql db 中,但是,当我尝试获取无法正确获取的图像时。如果你在 texteditor 中打开一张图片,它会是什么样子?同样的事情我现在得到了......像这样......(Q?f0±ó%“÷YúE?Vúo?e9°R}è”!5????\ˉ?????YU0T3?′? \aV?%zùBe??nPè Qx?e?o0′áPá”·=ó-)Oyá+×??S?T??S!Y¨;,?¤a5H?ó?íó3áo?j/"5± ?RX?ee.&{ +C?H Cìai,F+?#”?ì8??IDO%IW)。

my question is how to fetch the image properly? and then is there any other way to do store an image to mysql db. for example save a employee profile pictures to one folder and then store that link to mysql???

我的问题是如何正确获取图像?然后有没有其他方法可以将图像存储到mysql db。例如,将员工个人资料图片保存到一个文件夹,然后将该链接存储到 mysql ???

index.php code:

index.php 代码:

<form enctype="multipart/form-data" action="img.php" method="post" name="changer">
<input name="image" accept="image/jpeg" type="file">
<input value="Submit" type="submit">
</form>
<?php
include("config.php");
$query = mysql_query("SELECT * FROM tbl_images");
while($row = mysql_fetch_array($query))
{
    echo "" . $row['image'] . "";
}
?>

img.php code:

img.php 代码:

<?
include("config.php");
if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) { 

  $tmpName  = $_FILES['image']['tmp_name'];  

  $fp      = fopen($tmpName, 'r');
  $data = fread($fp, filesize($tmpName));
  $data = addslashes($data);
  fclose($fp);

  $query = "INSERT INTO tbl_images ";
  $query .= "(image) VALUES ('$data')";
  $results = mysql_query($query, $link) or die(mysql_error());

  print "Thank you, your file has been uploaded.";

  }
  else {
  print "No image selected/uploaded";
  }
?>

回答by Krish R

Can you try this,

你可以试试这个吗

 echo '<img src="data:image/png;base64,' . base64_encode($row['image']) . '" />';

Your code:

您的代码:

while($row = mysql_fetch_array($query))
{
   echo '<img src="data:image/png;base64,' . base64_encode($row['image']) . '" />';
}

回答by user3056620

please check the column size where you are saving the file content. The probable reason to me that you are not able to retrieve the images could be that the data is being trimmed due to size limit of the column. Try increasing the size of the column and changing the data type also, if needed. You may try the longblob datatype.

请检查您保存文件内容的列大小。对我来说,您无法检索图像的可能原因可能是由于列的大小限制而正在修剪数据。如果需要,尝试增加列的大小并更改数据类型。您可以尝试使用 longblob 数据类型。

回答by Satish Sharma

you must store you image in a folder and rename the file unique name along with the extension.

您必须将图像存储在一个文件夹中,并将文件的唯一名称与扩展名一起重命名。

and store filename in mysql database table. i think the given code will help you.

并将文件名存储在 mysql 数据库表中。我认为给定的代码会帮助你。

    <?
include("config.php");
if (isset($_FILES['image']) && $_FILES['image']['size'] > 0) { 

  $tmpName  = $_FILES['image']['tmp_name'];  

 $filename = $_FILES['image']['name'];
 $ext = getExtension($filename);
 $new_file = "uniquename_of_file.".$ext; // you can generate unique name by any way you want.

move_uploaded_file($_FILES['image']['tmp_name'],$DESTINATION_FOLDER."/".$new_file; // upload file to the destination folder with new name

  $query = "INSERT INTO tbl_images ";
  $query .= "(image) VALUES ('$new_file')";
  $results = mysql_query($query, $link) or die(mysql_error()); // insert new name into the database

  print "Thank you, your file has been uploaded.";

  }
  else {
  print "No image selected/uploaded";
  }
?>

function of getting extension will be as follows

获取扩展功能如下

     function getExtension($str) {
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
 }

回答by Stuart Miller

You need to have it rendered in the HTML as an img tag.

您需要将其作为 img 标签呈现在 HTML 中。

You should consider storing the image in a directory on the server and storing the image name/location in the database. Then create an img tag from that.

您应该考虑将图像存储在服务器上的目录中,并将图像名称/位置存储在数据库中。然后从中创建一个 img 标签。

回答by Shankar Damodaran

Encapsulate the echostatement in <img>tags

echo语句封装在<img>标签中

<form enctype="multipart/form-data" action="img.php" method="post" name="changer">
<input name="image" accept="image/jpeg" type="file">
<input value="Submit" type="submit">
</form>
<?php
include("config.php");
$query = mysql_query("SELECT * FROM tbl_images");
while($row = mysql_fetch_array($query))
{
    echo "<img src=". $row['image'] . "/>"; // Do like this
}
?>