PHP uses BLOB to access image information instances

Source: Internet
Author: User
PHP uses BLOB to access image information instances
  • PHP uses BLOB to access image information instances
  • BLOB introduction
  • Mysql BLOB type
  • Create a database
  • Php file to binary code
  • HTML code
  • Read binary image
  • Complete php code
  • HTML code
  • Introduction to accessing image information instance BLOB using BLOB in PHP

    BLOB (binary large object), a large binary object, is a container that can store binary files. In computer systems, BLOB is often the field type used to store binary files in databases. BLOB is a large file. a typical BLOB is an image or sound file. due to their size, it must be processed in a special way (for example: upload, download, or store data in a database ). According to Eric Raymond, the main idea of processing BLOB is to let the file processor (such as the database manager) ignore what the file is, but care about how to process it. However, some experts also stressed that this method of processing big data objects is a double-edged sword, which may lead to some problems, such as the storage of a large binary file, will degrade the database performance. Storing large multimedia objects in databases is a typical example of processing BLOB by applications.

    Mysql BLOB type
    In MySQL, BLOB is a type series, including TinyBlob, Blob, MediumBlob, and LongBlob. The only difference between these types is that the maximum size of the stored files varies.
    Type size (unit: bytes)
    TinyBlob 255 Max
    Blob Maximum 65 K
    MediumBlob Maximum 16 M
    LongBlob Maximum 4 GB
    In linux, modify etc/my. cnf [mysqld] max_allowed_packet = 16 M // different from max_allowed_packet under [mysqldump ].

    Note:Blob is a reserved word of the mysql database,Be sure not to use the field name.

    Create a database
    CREATE TABLE IF NOT EXISTS `myimg` ( `imgid` tinyint(3) NOT NULL AUTO_INCREMENT, `imgtype` varchar(25) NOT NULL DEFAULT '', `imgblob` mediumblob NOT NULL, PRIMARY KEY (`imgid`) )
    Php file to binary code

    The database reads, updates, and inserts data in the same way, so it does not need to be written. here, we only need to write the key code for uploading images to binary in PHP.

     0) {if (is_uploaded_file ($ _ FILES ['upimg '] ['tmp _ name']) {// Convert to binary $ imgBlob = addslashes (file_get_contents ($ _ FILES ['upimg '] ['tmp _ name']);}
    HTML code

    The file upload form must define enctype.

    enctype="multipart/form-data" 

    Complete Code

            
     
      
    Upload a file to Blob          
      

    Read binary 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.

    Complete php 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 reading this article and hope it will be helpful to php developers.
    PHP BloB full instance reference network 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.