Home > Backend Development > PHP Tutorial > PHP Tutorial: How to Use BLOB to Access Image Information Example_PHP Tutorial

PHP Tutorial: How to Use BLOB to Access Image Information Example_PHP Tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-13 10:41:33
Original
1528 people have browsed it

PHP Tutorial: How to Use BLOB to Access Image Information Example_PHP Tutorial

BLOB is a MySQL data type, called a binary large object. As its name suggests, it is used to store large amounts of string data similar to MYSQL binary and VARBINARY types.



MySQL BLOB classification



MySQL BLOB type maximum storage length (bytes)
TINYBLOB (1)(2^8)
blob ((2^16)1)
MEDIUMBLOB ((2^24)1)

LONGBLOB ((2 ^ 32)1)


In this tutorial, we learn how to insert and read MySQL BLOB fields using PHP.

(PS: T is good PHP Q buckle: 276167802, verification: csl)

First, we need to create a MySQL table with a BLOB field.

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`)
)
Copy after login


Insert data


Insert picture information into the MySQL BLOB field.


1. Upload image files.


2. Get image attributes (image data, image type, etc.)


3. Insert the image file into the BLOB.


PHP implementation script:


imageUpload.php

<?php
if(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>

</div>
</BODY>
</HTML>
Copy after login


After executing this script, the upload form will be displayed as follows:

PHP Tutorial: How to Use BLOB to Access Image Information Example_PHP Tutorial


Upon submitting the form, PHP fetches the file with the content image and stores it as binary data into a MySQL BLOB column.



Show picture


To display a BLOB image on the browser, we must:


1. Obtain image data and type from MySQL BLOB


2. Set the type to image (image/jpg, image/gif, ...) and use the PHP header() function.


3. Output the image content.


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);
?>
Copy after login


The above PHP code will display the picture stored in MySQL BLOB. From the HTML image tag we can refer to this PHP file with the corresponding image_id as parameter. For example:


<img  src="http://blog.csdn.net/u012275531/article/details/imageView.php?image_id=<?php echo $row["imageId"]; ? alt="PHP Tutorial: How to Use BLOB to Access Image Information Example_PHP Tutorial" >" />
Copy after login


The completed code is as follows:


listImages.php





List BLOB Images




<img  src="http://blog.csdn.net/u012275531/article/details/imageView.php?image_id=<?php echo $row["imageId"]; ? alt="PHP Tutorial: How to Use BLOB to Access Image Information Example_PHP Tutorial" >" />
Copy after login

The above is an example of how to use BLOB to access image information in this PHP tutorial. I hope this article will be helpful to the majority of PHP developers. Thank you for reading this article.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/678029.htmlTechArticleBLOB is a MySQL data type, called a binary large object. As its name suggests, it is used to store large amounts of string data of MYSQL-like binary and VARBINARY types. MySQL BLOB classification...
Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Why can't the list be rendered?
From 1970-01-01 08:00:00
0
0
0
HTML summary
From 1970-01-01 08:00:00
0
0
0
HTML summary
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template