Home >Backend Development >PHP Tutorial >PHP uses blob to access image information (including source code)
php uses blob to access image information (including source code)
BLOBYes 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 article In this tutorial, we learn how to insert and read MySQL BLOB fields using PHP.
(PS: T good PHP Q buckle: 276167802, verification: csl)
First, we need tocreate 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`) )
Insert data
Insert picture information into the MySQL BLOB field.
1. Upload the image file.
2. Get the image attributes (image data, image type, etc.)
3. Insert the image file into 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> <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>
After executing this script, the upload form will be displayed as follows:
Submit the form, PHP gets the file of the content image and stores it as binary data into a MySQL BLOB column.
Display pictures
To display BLOB images on the browser, we must:
1 , Obtain image data and type from MySQL BLOB
2. Set the type to image (image/jpg, image/gif, …) using 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); ?>
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="imageView.php?image_id=<?php echo $row["imageId"]; ?>" />
The completed code is as follows:
listImages.php
List BLOB Images <img src="imageView.php?image_id=<?php echo $row["imageId"]; ?>" />
Thank you for reading, I hope you will benefit a lot.
Original link: https://blog.csdn.net/u012275531/article/details/1791499
Recommended tutorial: "php tutorial"
The above is the detailed content of PHP uses blob to access image information (including source code). For more information, please follow other related articles on the PHP Chinese website!