PHP Tutorial on how to use BLOB to access image information instances

Source: Internet
Author: User
BLOB is a MySQL data type called a binary large object. Just like its name, it is used to store class & amp; 20284; MYSQL binary and VARBINARY type, a large amount of string data. MySQLBLOB classification MySQLBLOB type maximum storage length (bytes) TINYBLOB


BLOB isMySQLThe data type is called a binary large object. Just like its name, it is used to store a large amount of string data similar to MYSQL binary and VARBINARY.



MySQL BLOB classification



Maximum storage length (bytes) of MySQL BLOB type)
TINYBLOB (1) (2 ^ 8)
Blob (2 ^ 16) 1)
MEDIUMBLOB (2 ^ 24) 1)

LONGBLOB (2 ^ 32) 1)


In this tutorial, we will learn how to use PHPInsert and read MySQL BLOB fields.

(PS: T good PHPQ button? N: 276167802, verification: csl)

First, we need to create a MySQL table and 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 the image information into the MySQL BLOB field.


1. Upload an image file.


2. obtain Image attributes (image data, image type, and so on .)


3. Insert BLOB into the image file.


PHP implementation script:


ImageUpload. php

  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("Error: Problem on Image Insert
" .mysql_error());if(isset($current_id)) {header("Location: listImages.php");}}}?>Upload Image to MySQL BLOB


After the script is executed, the upload form is displayed as follows:




Submit the form. PHP obtains the file of the content Image and stores it as binary data to the MySQL BLOB column.



Show image


To display BLOB images in a browser, we must:


1. obtain image data and types from MySQL BLOB


2. set the image type to image (image/jpg, image/gif ,...) Use the PHP header () function.


3. output the image content.


ImageView. php

 Error: Problem on Retrieving Image BLOB
". 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 image stored in MySQL BLOB. From the HTML image tag, we can refer to the PHP file and the corresponding image_id as the parameter. For example:


" />


The complete code is as follows:


ListImages. php


 List BLOB Images
 
 " />

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.