How to use PHP to read binary files

Source: Internet
Author: User
Most of the time PHP reads binary files, data is not saved in text format. Therefore, binary data needs to be read and restored to the desired format. PHP also provides powerful support for binary processing.

Task

The following example describes how to use PHP to read and analyze a PNG image file header.

Involved functions

• Fopen

• Fread

• Unpack

• Bin2hex

PNG format overview

To complete the task, the following describes the PNG file format.

PNG is a lossless compression image file format. the 1-8 bytes of this format stores the PNG signature domain. the content is as follows:

• Decimal: 137 80 78 71 13 10 26 10

• Hexadecimal: 89 50 4e 47 0d 0a 1a 0a

Our task is to read this file header.

For more details about the PNG format:

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

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

Read files

PHP code

  1. $ FilePath = "icon.png ";
  2. // You must use rb to read files. This ensures the security of cross-platform binary data reading.
  3. $ Fh = fopen ($ filePath, "rb ");
  4. // Read only the first 8 bytes
  5. $ Head = fread ($ fh, 8 );
  6. Fclose ($ fh );

The above code has read the required 8 bytes into the variable head. Head is an array for storing binary data. we also need to perform some operations on it to obtain the data we need.

Unpack

Unpack can parse binary data into a relational array. it accepts two parameters. The first one provides a parsing string (see below ), the second parameter provides the head variable we read earlier.

• A: NULL-filled byte string

• A: Space-filled byte string

• H: hexadecimal number, four-bit lower priority

• H: hexadecimal number, with 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-tail byte order)

• V: unsigned short integer (always 16 bits, in the smallest byte order)

• I: signed integer (machine-related size and byte order)

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

• L: Signed long integer (always 32-bit, machine byte order)

• L: unsigned long integer (always 32-bit, machine byte order)

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

• V: unsigned long integer (always 32-bit, in the smallest byte order)

• F: floating point number (machine-related size and representation)

• D: double precision (machine-related size and representation)

• X: null bytes

• X: regressing a byte

@: Fill in the absolute position with NULL

The first unpack parameter has a little tips in usage. The following is an example:

• C reads 1 character, and the returned array index is 1

• C4 reads four bytes, one character for each byte, and returns an array index of 1, 2, 4

• C4head reads four characters, one character per byte. the returned array indexes are head1, head2, head3, and head4.

• Chead reads 1 character, and the returned array index is head

Now try to read 1st bytes:

PHP code

  1. $ Arr = unpack ("Chead", $ head );
  2. Print_r ($ arr );
  3. // Array ([head] = & gt; 137)

Read all 8 bytes, which can be separated by a slash:

PHP code

  1. $ Arr = unpack ("Chead/C3string/C4number", $ head );
  2. Print_r ($ arr );

Splice Keys starting with string into strings:

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 content printed using print_r is a decimal number. if you want to directly obtain the hexadecimal value, you can 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.