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 uses BLOB to access picture information instances

    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

    In MySQL, BLOBs are a series of types, including: Tinyblob, BLOBs, Mediumblob, Longblob, the only difference between these types is the maximum size of the storage file.
    Type size (units: bytes)
    Tinyblob Maximum 255
    Blob 65K Max
    Mediumblob 16M Max
    Longblob 4G Max
    Linux Modified Etc/my.cnf[mysqld]max_allowed_packet = 16M//differs from the max_allowed_packet under [mysqldump]

    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 ' tinyint (3) NOT NULL auto_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

     
      0) {      if (is_uploaded_file ($_files[' upimg '] [' tmp_name '])) {    //turn into binary    $imgBlob =addslashes (file_get_ Contents ($_files[' upimg ' [' tmp_name ']));  }  

    HTML code

    The upload file form must be defined Enctype

    Full code

            
     
      
       
      Uploading files to BLOBs                
     
      

    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

     
      Error:sql Statement Error
    ". Mysql_error ()); $row = Mysql_fetch_array ($result); Header ("Content-type:".) $row ["ImageType"]); echo $row ["ImageData"]; } Mysql_close ($conn); ? >

    HTML code

    "/>

    Thanks for viewing this article, I hope you can help PHP developers.
    PHP BLOB full Instance reference network article
    ??

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