PHP uses BLOB to access picture information instances

Source: Internet
Author: User

    • PHP uses BLOB to access picture information instances
      • BLOB Introduction
        • MySQL blob type
        • Create a database
      • PHP file into binary code
      • HTML code
      • Read binary picture
        • PHP Full Code
        • HTML code

PHP using BLOB Access image information Instance blob introduction

BLOB (Binary large object), binary large, is a container that can store binary files. In computers, blobs are often the type of field used in a database to store binary files. A blob is a large file, a typical blob is a picture or a sound file, and due to its size, it must be handled in a special way (for example, uploading, downloading, or storing to a database). According to Eric Raymond, the main idea of dealing with blobs is to let a file processor (such as a database manager) ignore what the file is, but rather care about how to handle it. But there are also experts who stress that the way to handle big data objects is to double-edged swords, which may cause problems such as storing binary files too large, which can degrade the performance of the database. Storing large multimedia objects in a database is a typical example of application processing blobs.

MySQL blob type
MySQL中,BLOB是个类型系列,包括:TinyBlob、Blob、MediumBlob、LongBlob,这几个类型之间的唯一区别是在存储文件的最大大小上不同。
type size (units: bytes)
Tinyblob Maximum 255
Blob 65K Max
Mediumblob 16M Max
Longblob 4G Max
linux修改etc/my.cnf[mysqld]max_allowed_packet = 16M //不同于[mysqldump]下的max_allowed_packet

Note: The blob is a MySQL database reserved word, so be sure not to use it for field names .

Create a database
CREATE TABLE IF  not EXISTS ' myimg '(' Imgid 'tinyint3) not NULLAuto_increment,' Imgtype ' varchar( -) not NULL DEFAULT "',' Imgblob 'Mediumblob not NULL,PRIMARY KEY(' Imgid ')  )
PHP file into binary code

Database read update insert is the same, do not write, here only to write PHP upload image to binary key code

<?php  if(count($_FILES0) {      if(is_uploaded_file($_FILES[‘upimg‘][‘tmp_name‘])) {    // 转成二进制    $imgBlob =addslashes(file_get_contents($_FILES[‘upimg‘][‘tmp_name‘]));  }  
HTML code

The upload file form must be defined Enctype

Full code

<html>  <head>      <titel>Uploading files to BLOBs</title></head>  <body>      <form name="Formimage" enctype="Multipart/form-data"  Action="" method="POST">          <label>Upload file:</label>        <input name="upimg" type="file"/>          <input type="Submit" value="Submit"/>      </form>  </div>  </body>  </html>  
Read binary picture

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.

PHP Full Code

File name: imageview.php

<?php  $conn= Mysql_connect ("localhost","Root",""); mysql_select_db ("Myimg")or  die(Mysql_error ());if(isset($_get[' image_id '])) {$sql="Select Imgtype,imgblob from myimg WHERE imgid=".$_get[' Imgid '];$result= mysql_query ("$sql")or  die("<b>error:</b>sql statement error <br/>". Mysql_error ());$row= Mysql_fetch_array ($result); Header"Content-type:".$row["ImageType"]);Echo $row["ImageData"]; } mysql_close ($conn);?>
HTML code
<img src="imageView.php?image_id=<?php echo $row["imageId"]; ?>" />

Thanks for viewing this article, I hope you can help PHP developers.
PHP BLOB full Instance reference network article
--http://blog.csdn.net/u012275531/article/details/17914999

PHP uses BLOB to access picture information instances

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.