当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


PHP File Content转Byte Array用法及代码示例


将文件内容转换为字节数组PHP是一种适用于各种应用程序的有用技术,包括文件操作、数据处理以及处理图像或 PDF 等二进制文件。 PHP 提供了多种读取文件内容并将其转换为字节数组的方法,提供了有效处理不同场景的灵活性。

使用file_get_contents()unpack() 函数

在 PHP 中将文件内容转换为字节数组的最简单方法之一是使用file_get_contents()将文件内容读取为字符串,然后使用unpack()将字符串转换为字节数组。

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); 
  
?>
输出
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...

使用fread()unpack() 函数

对于较大的文件,或者当您需要对读取过程进行更多控制时(例如分块读取),恐惧结合福彭打开包装可以使用。

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); 
  
?>

输出

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 )

这里,fopen以二进制读取模式打开文件(‘rb’),filesize获取文件的大小,fread根据提供的大小读取文件内容。 unpack 然后将此内容转换为字节数组。

使用文件和数组映射

另一种方法是将文件读入行数组中文件,然后使用array_map将每行的每个字符转换为其相应的字节值。

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); 
?>

输出

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 )



相关用法


注:本文由纯净天空筛选整理自vkash8574大神的英文原创作品 How to Convert File Content to Byte Array in PHP ?。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。