Php method for reading binary files

Source: Internet
Author: User
Most of the time, the data is not stored in the form of text, which requires that binary data be read out and restored to the format we need. PHP also provides strong support for binary processing.

Task

The following is an example of reading and parsing a file header for a PNG image, explaining how to read and parse a binary file using PHP.

involved functions

fopen

fread

Unpack

Bin2Hex

Introduction to PNG format

In order to complete the task, the following is a brief introduction to the PNG file format.

PNG is a lossless compressed image file format in which the 第1-8 byte holds the PNG signature domain, with the following content:

• Decimal: 137 80 78 71 13 10 26 10

• 16 binary: 4e 0d 0a 1a 0a

Our task is to read the header of this file.

A more detailed introduction to the PNG format:

http://www.w3.org/TR/2003/REC-PNG-20031110/

http://www.libpng.org/pub/png/

Read file

PHP code

    1. $filePath = "Icon.png";
    2. You must use RB to read the file, which guarantees read-safe cross-platform binary data
    3. $fh = fopen ($filePath, "RB");
    4. Read only the previous 8 bytes
    5. $head = Fread ($fh, 8);
    6. Fclose ($FH);

The above code has already read the 8 bytes we need into the variable head. Head is an array of binary data, and we need to do something about it to get the data we need.

Unpack

Unpack can parse binary data into a relational array, which accepts 2 parameters, the first provides a parse string (see below), and the second parameter provides the head variable that we read earlier.

A:null populated byte string

A: A space-filled byte string

H: Hexadecimal number, low four-byte priority

H: Hexadecimal number, high four-byte precedence

C: Signed characters

C: Unsigned characters

• S: signed short integer (always 16 bits, machine byte-order)

• S: unsigned short integer (always 16 bits, machine byte-order)

• N: unsigned short integer (always 16-bit, large-tailed byte-order)

V: unsigned short integer (always 16-bit, small-tailed byte-order)

• I: Signed integer (machine-relative size and byte-order)

• I: unsigned integer (machine-relative size and byte-order)

L: Signed Long Integer (always 32 bits, machine byte-order)

l: unsigned long integer (always 32 bits, machine byte-order)

• N: unsigned long integer (always 32-bit, large-tailed byte-order)

V: unsigned long integer (always 32-bit, small-tailed byte-order)

F: Floating point number (machine-relative size and representation)

d: Double precision number (machine-relative size and representation)

• x: null byte

• x: Backwards one byte

@: Fill absolute position with null

The first parameter of unpack is a little bit tricky in use, here's an example:

C reads 1 characters and returns an array index of 1

C4 reads 4 bytes, one character per byte, and returns an array index of 1,2,3,4

C4head reads 4 characters, one character per byte, and returns an array index of HEAD1,HEAD2,HEAD3,HEAD4

Chead reads 1 characters and returns the array index to head

Now try to read the 1th byte:

PHP code

    1. $arr = Unpack ("Chead", $head);
    2. Print_r ($arr);
    3. Array ([head] = 137)

Read all 8 bytes, separated by a slash:

PHP code

    1. $arr = Unpack ("Chead/c3string/c4number", $head);
    2. Print_r ($arr);

To string a key that starts with a string:

PHP code

    1. $arr = Unpack ("Chead/c3string/c4number", $head);
    2. for ($i =1; $i <=3; $i + +)
    3. {
    4. $type. = Chr ($arr [' string '. $i]);
    5. }
    6. Echo $type;
    7. Png

Bin2Hex

The above uses Print_r to print out the content, all is the decimal number, if wants to obtain the hexadecimal value directly, may use the Bin2Hex function.

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