Open In App

How to Convert File Content to Byte Array in PHP ?

Last Updated : 14 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Converting file content to a byte array in PHP is a useful technique for various applications, including file manipulation, data processing, and when working with binary files like images or PDFs. PHP offers multiple ways to read file content and convert it into a byte array, providing flexibility to handle different scenarios effectively.

Using file_get_contents() and unpack() Functions

One of the simplest ways to convert file content to a byte array in PHP is by using the file_get_contents() function to read the file content as a string, and then using unpack() function to convert the string to a byte array.

PHP




<?php
  
$fileContent = file_get_contents($filePath);
  
// Convert the file content to a byte array
$byteArray = unpack('C*', $fileContent);
  
// Output the byte array
print_r($byteArray);
  
?>


Output

Array
(
    [1] => 60
    [2] => 115
    [3] => 118
    [4] => 103
    [5] => 32
    [6] => 120
    [7] => 109
    [8] => 108
    [9] => 110
    [10] => 115
    [11] => 61
    [12] => 34
    [13] => 1...

Using fread() and unpack() Functions

For larger files, or when you need more control over the reading process (such as reading in chunks), fread in combination with fopen and unpack can be used.

PHP




<?php
  
$filePath = 'gfg.txt';
  
$fileHandle = fopen($filePath, 'rb');
$fileSize = filesize($filePath);
$fileContent = fread($fileHandle, $fileSize);
  
$byteArray = unpack('C*', $fileContent);
  
fclose($fileHandle);
  
print_r($byteArray);
  
?>


Output

Array ( [1] => 87 [2] => 101 [3] => 108 [4] => 99 [5] => 111 [6] => 109 [7] => 101 [8] => 32 [9] => 116 [10] => 111 [11] => 32 [12] => 71 [13] => 101 [14] => 101 [15] => 107 [16] => 115 [17] => 102 [18] => 111 [19] => 114 [20] => 71 [21] => 101 [22] => 101 [23] => 107 [24] => 115 [25] => 13 [26] => 10 [27] => 87 [28] => 101 [29] => 108 [30] => 99 [31] => 111 [32] => 109 [33] => 101 [34] => 32 [35] => 116 [36] => 111 [37] => 32 [38] => 71 [39] => 101 [40] => 101 [41] => 107 [42] => 115 [43] => 102 [44] => 111 [45] => 114 [46] => 71 [47] => 101 [48] => 101 [49] => 107 [50] => 115 [51] => 13 [52] => 10 [53] => 72 [54] => 101 [55] => 108 [56] => 108 [57] => 111 [58] => 32 [59] => 87 [60] => 101 [61] => 108 [62] => 99 [63] => 111 [64] => 109 [65] => 101 )

Here, fopen opens the file in binary read mode (‘rb’), filesize gets the size of the file, and fread reads the file content based on the provided size. unpack then converts this content into a byte array.

Using File and Array Mapping

Another approach involves reading the file into an array of lines with file, then using array_map to convert each character of every line into its corresponding byte value.

PHP




<?php
  
$filePath = 'gfg.txt';
  
$lines = file($filePath, FILE_IGNORE_NEW_LINES);
  
// Convert each line's characters to bytes
// and merge into one byte array
$byteArray = array_merge([], ...array_map(function($line) {
    return array_map('ord', str_split($line));
}, $lines));
  
// Output the byte array
print_r($byteArray);
?>


Output

Array ( [0] => 87 [1] => 101 [2] => 108 [3] => 99 [4] => 111 [5] => 109 [6] => 101 [7] => 32 [8] => 116 [9] => 111 [10] => 32 [11] => 71 [12] => 101 [13] => 101 [14] => 107 [15] => 115 [16] => 102 [17] => 111 [18] => 114 [19] => 71 [20] => 101 [21] => 101 [22] => 107 [23] => 115 [24] => 87 [25] => 101 [26] => 108 [27] => 99 [28] => 111 [29] => 109 [30] => 101 [31] => 32 [32] => 116 [33] => 111 [34] => 32 [35] => 71 [36] => 101 [37] => 101 [38] => 107 [39] => 115 [40] => 102 [41] => 111 [42] => 114 [43] => 71 [44] => 101 [45] => 101 [46] => 107 [47] => 115 [48] => 72 [49] => 101 [50] => 108 [51] => 108 [52] => 111 [53] => 32 [54] => 87 [55] => 101 [56] => 108 [57] => 99 [58] => 111 [59] => 109 [60] => 101 )



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads