简体   繁体   English

从mysql php获取图像

[英]fetch image from mysql php

here i am trying to upload the employee profile picture to mysql and then i want to fetch those picture. 在这里,我试图将员工资料图片上传到mysql,然后我要获取这些图片。 now i can uploaded the image into mysql db successfully but, when i try to fetch the image that doesn't fetch properly. 现在,我可以成功将图像上传到mysql db,但是当我尝试获取无法正确获取的图像时。 if you open an image in texteditor na how it will look like? 如果您在texteditor na中打开图像,它将是什么样? the same thing i got it now.. like this... (QÕf0±Ó%"÷YúEûVÚo¾e9°R}È"!5®?•Ÿ\\¯ž›ÃÕîYU0T?³¼´œ\\aVë%z€ÙBðŒÆnPÈ Qx•eú0´ÁPÁ”·=ó-)Oyá+×½ÄS„ÞšÅS!Y¨;,¾¤ª5HÅÓôÍó?3Áº¶j/"5±•RX›ee.&{ +C˜ H CÌai,F+Ô#”?Ì8««IÐO?%IW). 我现在也得到了同样的东西..像这样...(QÕf0±Ó%“÷YúEûVÚo¾e¾e9°R}È”!5®?•Ÿ\\¯››ÕYU0T?³¼´œ \\aVë%z€ÙBðŒÆnPÈQx• eÃ0ÁÁPÁ”·=ó-)Oyá+×½ÄS„ÞšÅS!Y¨;,¾¤ª5HÅÓôÍó?3Áº¶j/“ 5±•RX› ee。&{+ C〜HCÌai,F +Ô#”? «8««IÐO?%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. 然后还有其他方法可以将映像存储到mysql db。 for example save a employee profile pictures to one folder and then store that link to mysql??? 例如将员工资料图片保存到一个文件夹,然后将该链接存储到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";
  }
?>

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']) . '" />';
}

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数据类型。

Encapsulate the echo statement 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
}
?>

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标签。

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. 并将文件名存储在mysql数据库表中。 i think the given code will help you. 我认为给定的代码将帮助您。

    <?
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;
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM