从mysql php获取图像 [英] fetch image from mysql php
问题描述
这里我试图将员工个人资料图片上传到mysql然后我想要获取这些图片。现在我可以成功将图像上传到mysql db中,但是当我尝试获取无法正常获取的图像时。如果你在texteditor中打开图像,它会是什么样子?我现在得到了同样的东西......就像这样...(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〜HCÌai,F +Ô #?Ì8««IÐO?%IW)。
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®?•Ÿ\¯ž›ÃÕî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).
我的问题是如何正确获取图像?然后还有其他方法可以存储图像到mysql db。例如将员工个人资料图片保存到一个文件夹,然后将该链接存储到mysql ???
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???
index.php代码:
index.php code:
<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代码:
img.php code:
<?
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";
}
?>
推荐答案
你能试试吗,
echo '<img src="data:image/png;base64,' . base64_encode($row['image']) . '" />';
您的代码:
while($row = mysql_fetch_array($query))
{
echo '<img src="data:image/png;base64,' . base64_encode($row['image']) . '" />';
}
这篇关于从mysql php获取图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!