PHP tutorial How to use BLOB to access picture information instance ____php

Source: Internet
Author: User
Tags php tutorial


A blob is a MySQL data type called a binary large object. As its name it is used to store a large number of string data similar to the MySQL binary and varbinary type.

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 learned how to insert and read a MySQL blob field using PHP .

(Ps:t good PHP Q-Buckle 峮: 276167802, Validation: 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


Inserts picture information into the MySQL blob field.


1, upload image files.


2, Get Image properties (image data, image types, etc.). )


3, image file insertion 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>


The upload form after executing this script will appear as follows:




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



Show pictures


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


1. Get image data and type from MySQL blob


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


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


 "


The complete code is as follows:


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>
<?php
while ($row = Mysql_fetch_array ($result)) {
?>
<br/>
<?php
}
mysql_close ($conn);
? >
</BODY>
</HTML>

The above is this article about PHP tutorial how to use BLOB to access picture Information example, I hope this article for the vast number of PHP developers to help, 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.