您的位置 首页 php

php使用blob存取图片的信息(含源码)

php使用blob存取图片的信息(含源码)

php使用blob存取图片的信息(含源码)

BLOB是一种MySQL数据类型,称为二进制大对象。正如它的名字它是用来存储类似MYSQL二进制和VARBINARY类型的、大量的字符串数据。

MySQL BLOB分类

MySQL BLOB类型 最大存储长度(字节)

TINYBLOB (1)(2 ^ 8)

blob ((2 ^ 16)1)

MEDIUMBLOB ((2 ^ 24)1)

LONGBLOB ((2 ^ 32)1)

在这篇教程中,我们学习如何使用PHP插入和读取MySQL BLOB字段。

(PS:T不错的PHP Q扣峮:276167802,验证:csl)

首先,我们需要创建一个MySQL表与一个BLOB字段

CREATE TABLE IF NOT EXISTS `output_images` (  `imageId` tinyint(3) NOT NULL AUTO_INCREMENT,  `imageType` varchar(25) NOT NULL DEFAULT '',  `imageData` mediumblob NOT NULL,  PRIMARY KEY (`imageId`))

插入数据

将图片信息插入MySQL BLOB字段中。

1、上传图像文件.

2、获取图像属性(图像数据、图像类型等等。)

3、图像文件插入BLOB。

PHP实现脚本:

imageUpload.php

<?phpif(count($_FILES) > 0) {if(is_uploaded_file($_FILES['userImage']['tmp_name'])) {mysql_connect("localhost", "root", "");mysql_select_db ("phppot_examples");$imgData =addslashes(file_get_contents($_FILES['userImage']['tmp_name']));$imageProperties = getimageSize($_FILES['userImage']['tmp_name']);$sql = "INSERT INTO output_images(imageType ,imageData)VALUES('{$imageProperties['mime']}', '{$imgData}')";$current_id = mysql_query($sql) or die("<b>Error:</b> Problem on Image Insert<br/>" .mysql_error());if(isset($current_id)) {header("Location: listImages.php");}}}?><HTML><HEAD><TITLE>Upload Image to MySQL BLOB</TITLE><link href="imageStyles.css" rel="stylesheet" type="text/css" /></HEAD><BODY><form name="frmImage" enctype="multipart/form-data" action="" method="post"class="frmImageUpload"><label>Upload Image File:</label><br/><input name="userImage" type="file" class="inputFile" /><input type="submit" value="Submit" class="btnSubmit" /></form></div></BODY></HTML>

执行这个脚本后上传表单将显示如下:

提交表单,PHP获取内容图像的文件并将其作为二进制数据存储到MySQL BLOB列。

显示图片

在浏览器上显示BLOB图像,我们必须:

1、从MySQL BLOB获得图像数据和类型

2、将类型设置为图像(image/jpg, image/gif, …)使用PHP header()函数。

3、输出图像内容。

imageView.php<?php$conn = mysql_connect("localhost", "root", "");mysql_select_db("phppot_examples") or die(mysql_error());if(isset($_GET['image_id'])) {$sql = "SELECT imageType,imageData FROM output_images WHERE imageId=" . $_GET['image_id'];$result = mysql_query("$sql") or die("<b>Error:</b> Problem on Retrieving Image BLOB<br/>". mysql_error());$row = mysql_fetch_array($result);header("Content-type: " . $row["imageType"]);echo $row["imageData"];}mysql_close($conn);?>

上面的PHP代码将显示MySQL BLOB存储的图片。从HTML图像标签我们可以参考这个PHP文件与相应image_id作为参数。例如:

<img src="imageView.php?image_id=<?php echo $row["imageId"]; ?>" />

完成代码如下:

listImages.php

<?php$conn = mysql_connect("localhost", "root", "");mysql_select_db("phppot_examples");$sql = "SELECT imageId FROM output_images ORDER BY imageId DESC";$result = mysql_query($sql);?><HTML><HEAD><TITLE>List BLOB Images</TITLE><link href="imageStyles.css" rel="stylesheet" type="text/css" /></HEAD><BODY><?phpwhile($row = mysql_fetch_array($result)) {?><img src="imageView.php?image_id=<?php echo $row["imageId"]; ?>" /><br/><?php}mysql_close($conn);?></BODY></HTML>

感谢大家的阅读,希望大家收益多多。

原文链接:https://blog.csdn.net/u012275531/article/details/1791499

推荐教程:《php教程》

以上就是php使用blob存取图片的信息(含源码)的详细内容,更多请关注求知技术网其它相关文章!

文章来源:智云一二三科技

文章标题:php使用blob存取图片的信息(含源码)

文章地址:https://www.zhihuclub.com/6557.shtml

关于作者: 智云科技

热门文章

评论已关闭

4条评论

  1. Pretty! This was a really wonderful post. Many thanks for supplying this info.

  2. I wanted to thank you for this very good read!! I certainly enjoyed every bit of
    it. I have got you bookmarked to check out new stuff you post…

  3. Oh my goodness! Impressive article dude! Thank you, However I am
    experiencing troubles with your RSS. I don’t know the
    reason why I cannot join it. Is there anyone else getting similar RSS
    issues? Anyone who knows the answer can you kindly respond?

    Thanx!!

  4. Hey There. I found your blog using msn. This is a
    really well written article. I will be sure to bookmark
    it and come back to read more of your useful information. Thanks for the post.

    I will certainly return.

网站地图