當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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 ?。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。