简体   繁体   English

PHP将大二进制文件转换为可读文件

[英]PHP Convert a big binary file to a readable file

I am having problems with this simple script... 我对这个简单的脚本有问题......

<?php
$file = "C:\\Users\\Alejandro\\Desktop\\integers\\integers";

$file = file_get_contents($file, NULL, NULL, 0, 34359738352);

echo $file;
?>

It gives me always this error: 它总是给我这个错误:

file_get_contents(): length must be greater than or equal to zero file_get_contents():length必须大于或等于零

I am really tired, can someone help me? 我真的很累,能有人帮助我吗?

EDIT: After dividing the file in 32 parts... 编辑:将文件分成32份后...

The error is 错误是

PHP Fatal error:  Allowed memory size of 1585446912 bytes exhausted (tried to al
locate 2147483648 bytes)

Fatal error: Allowed memory size of 1585446912 bytes exhausted (tried to allocat
e 2147483648 bytes)

EDIT (2): 编辑(2):

Now the deal is to convert this binary file into a list of numbers from 0 to (2^31)-1 That is why I need to read the file, so I can convert the binary digits to decimal numbers. 现在的交易是将这个二进制文件转换为从0到(2 ^ 31)-1的数字列表。这就是为什么我需要读取文件,所以我可以将二进制数字转换为十进制数字。

The last argument is an int , and apparently in your implementation of PHP int only has 31 bits for magnitude. 最后一个参数是一个int ,显然在你的PHP实现中int只有31位的大小。 The number you provide is larger than that, so this call would never work on that system. 您提供的数字大于此数字,因此此调用永远不会在该系统上运行。

There are also other peculiarities: 还有其他特点:

  • why do you pass NULL for the second argument instead of false ? 为什么你为第二个参数传递NULL而不是false
  • you are saying that you want to read up to more than 2GB from that file -- is the system and PHP configuration up to doing that? 你是说你要从该文件中读取超过2GB的内容 - 系统和PHP配置是否可以实现?

Looks like your file path is missing a backslash, which could be causing file_load_contents() to fail loading the file. 看起来您的文件路径缺少反斜杠,这可能导致file_load_contents()无法加载文件。

Try this instead: 试试这个:

$file = "C:\\\\Users\\Alejandro\\Desktop\\integers\\integers";

EDIT: Now that you can read your file, we're finding that because your file is so large, it's exceeding your memory limit and causing your script to crash. 编辑:既然你可以读取你的文件,我们发现因为你的文件太大,它超出你的内存限制并导致你的脚本崩溃。

Try buffering one piece at a time instead, so as not to exceed memory limits. 尝试一次缓冲一件,以免超出内存限制。 Here's a script that will read binary data, one 4-byte number at a time, which you can then process to suit your needs. 这是一个脚本,它将读取二进制数据,一次一个4字节的数字,然后您可以根据需要进行处理。

By buffering only one piece at a time, you allow PHP to use only 4 bytes at a time to store the file's contents (while it processes each piece), instead of storing the entire contents in a huge buffer and causing an overflow error. 通过一次只缓冲一个部分,允许PHP一次只使用4个字节来存储文件的内容(当它处理每个部分时),而不是将整个内容存储在一个巨大的缓冲区中并导致溢出错误。

Here you go: 干得好:

$file = "C:\\\\Users\\Alejandro\\Desktop\\integers\\integers";

// Open the file to read binary data
$handle = @fopen($file, "rb"); 
if ($handle) { 
   while (!feof($handle)) { 
       // Read a 4-byte number
       $bin = fgets($handle, 4);
       // Process it
       processNumber($bin); 
   } 
   fclose($handle); 
}

function processNumber($bin) {
    // Print the decimal equivalent of the number
    print(bindec($bin)); 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM