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
- $ FilePath = "icon.png ";
- // You must use rb to read files. This ensures the security of cross-platform binary data reading.
- $ Fh = fopen ($ filePath, "rb ");
- // Read only the first 8 bytes
- $ Head = fread ($ fh, 8 );
- 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
- $ Arr = unpack ("Chead", $ head );
- Print_r ($ arr );
- // Array ([head] = & gt; 137)
Read all 8 bytes, which can be separated by a slash:
PHP code
- $ Arr = unpack ("Chead/C3string/C4number", $ head );
- Print_r ($ arr );
-
Splice Keys starting with string into strings:
PHP code
- $ Arr = unpack ("Chead/C3string/C4number", $ head );
- For ($ I = 1; $ I <= 3; $ I ++)
- {
- $ Type. = chr ($ arr ['string'. $ I]);
- }
- Echo $ type;
- // 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.