How to Convert File Content to Byte Array in PHP ?
Last Updated :
16 Jul, 2024
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
$filePath = 'https://media.geeksforgeeks.org/wp-content/uploads/20190602174645/hello.txt';
$fileContent = file_get_contents($filePath);
// Convert the file content to a byte array
$byteArray = unpack('C*', $fileContent);
// Output the byte array
print_r($byteArray);
?>
OutputArray
(
[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 )
Using file and str_split Functions
Another efficient way to convert file content to a byte array in PHP involves reading the file into a string using the file_get_contents function, then converting this string to an array of characters using the str_split function. Finally, we use array_map to convert each character to its corresponding byte value.
Example: The following example show how to use file_get_contents, str_split, and array_map to convert file content to a byte array in PHP.
PHP
<?php
function fileToByteArray($filePath) {
$fileContent = file_get_contents($filePath);
$charArray = str_split($fileContent);
$byteArray = array_map('ord', $charArray);
return $byteArray;
}
$filePath = 'example.txt';
$byteArray = fileToByteArray($filePath);
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 )
Similar Reads
How to convert an array to CSV file in PHP ? To convert an array into a CSV file we can use fputcsv() function. The fputcsv() function is used to format a line as CSV (comma separated values) file and writes it to an open file. The file which has to be read and the fields are sent as parameters to the fputcsv() function and it returns the leng
2 min read
How to Append Data to a File in PHP? Appending data to a file is a common operation in PHP when you want to add new information without overwriting the existing content. This can be particularly useful for logging, saving user activity, or adding records incrementally. We will explore different approaches to appending data to a file in
2 min read
Convert byte[] array to File using Java As we know whenever it comes to writing over a file, write() method of the File class comes into play but here we can not use it in order to convert that byte into a file. In order to convert a byte array to a file, we will be using a method named the getBytes() method of String class. Implementatio
3 min read
How to Convert Base64 to File in JavaScript? In web development, Base64 encoding is often used to represent binary data, such as images or files, with a string of ASCII characters, sometimes you may be required to change this Base64 string back into a file for instance for file uploads, downloads, or processing in the browser, this article aim
2 min read
How to Convert JSON file into CSV in PHP ? In this article, we are going to see how to convert JSON data into a CSV file using PHP. JSON (JavaScript Object Notation) is a dictionary-like notation that can be used to structuring data. It is stored with the extension .json, for example - geeksforgeeks.json On the other hand, CSV (or Comma Sepa
2 min read
How to attach file with feedback form in PHP ? Feedback form is used to get the review from the user through mail services. Mailing is one of the server-side utilities that is required in most of the web servers today. In PHP, mail() is the built-in function that is used to send emails from PHP scripts either in a plain-text form or formatted HT
4 min read
How to Convert Base64 to Blob in JavaScript? Working with files and data in web applications often involves dealing with binary data. One common scenario is converting a Base64 string into a Blob object, which can then be used in various ways, such as creating downloadable files or uploading images to a server. This article will guide you thro
4 min read
How to upload a file in PHP ? In this article, we will learn how to upload a file using PHP. Let us first understand some basic configurations. Approach: In your "php.ini" file, search for the "file_uploads" parameter and set it to "On" as mentioned below. file_uploads = On In the "index.html" file, the enctype must be multipart
3 min read
How to check the Type and Size before File Uploading in PHP ? In PHP, it's essential to validate file types and sizes before allowing users to upload files to a server. This helps prevent potential security vulnerabilities and ensures that only allowed file types and sizes are accepted. PHP provides various methods to perform these validations, including check
2 min read
How to handle the warning of file_get_contents() function in PHP ? The file_get_contents() function in PHP is an inbuilt function which is used to read a file into a string. The function uses memory mapping techniques which are supported by the server and thus enhances the performances making it a preferred way of reading contents of a file. The path of the file to
3 min read