PHP tutorial How to use BLOB access picture information Instance _php Tutorial

Source: Internet
Author: User

A blob is a type of MySQL data, called a binary large object. As its name it is used to store a large number of string data like 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 use the PHPInsert and read the MySQL blob field.

(Ps:t good PHPQ-Buckle 峮: 276167802, verify: CSL)

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

CREATE TABLE IF not EXISTS ' output_images ' (  ' imageId ' tinyint (3) is not NULL auto_increment,  ' imageType ' varchar (25 Not null DEFAULT "',  ' imageData ' mediumblob not NULL,  PRIMARY KEY (' imageId '))


Inserting data


Insert the picture information into the MySQL blob field.


1, upload the image file.


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


3. Insert the image file into the blob.


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 executing this script, the upload form will appear as follows:




Submit the form, PHP gets the file of the content image and stores it as binary data to the MySQL BLOB column.



Show pictures


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


1. Get image data and types from MySQL blob


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


3, Output 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 a picture of the MySQL blob storage. From the HTML image tag we can refer to this PHP file with corresponding image_id as parameters. For example:


"/>


The completion code is as follows:


listimages.php


 
  List BLOB Images
 
  
 
  "/>

The above is the PHP tutorial on how to use BLOB access image information examples, I hope this article to the vast number of PHP developers to help, thanks to read this article.

http://www.bkjia.com/PHPjc/678029.html www.bkjia.com true http://www.bkjia.com/PHPjc/678029.html techarticle A blob is a type of MySQL data, called a binary large object. As its name it is used to store a class of MySQL binary and varbinary type, a large number of string data. MySQL BLOB Classification ...

  • 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.