PHP Convert a big binary file to a readable file -
i having problems simple script...
<?php $file = "c:\\users\\alejandro\\desktop\\integers\\integers"; $file = file_get_contents($file, null, null, 0, 34359738352); echo $file; ?>
it gives me error:
file_get_contents(): length must greater or equal zero
i tired, can me?
edit: after dividing file in 32 parts...
the error
php fatal error: allowed memory size of 1585446912 bytes exhausted (tried al locate 2147483648 bytes) fatal error: allowed memory size of 1585446912 bytes exhausted (tried allocat e 2147483648 bytes)
edit (2):
now deal convert binary file list of numbers 0 (2^31)-1 why need read file, can convert binary digits decimal numbers.
looks file path missing backslash, causing file_load_contents()
fail loading file.
try instead:
$file = "c:\\\\users\\alejandro\\desktop\\integers\\integers";
edit: can read file, we're finding because file large, it's exceeding memory limit , causing script crash.
try buffering 1 piece @ time instead, not exceed memory limits. here's script read binary data, 1 4-byte number @ time, can process suit needs.
by buffering 1 piece @ time, allow php use 4 bytes @ time store file's contents (while processes each piece), instead of storing entire contents in huge buffer , causing overflow error.
here go:
$file = "c:\\\\users\\alejandro\\desktop\\integers\\integers"; // open file read binary data $handle = @fopen($file, "rb"); if ($handle) { while (!feof($handle)) { // read 4-byte number $bin = fgets($handle, 4); // process processnumber($bin); } fclose($handle); } function processnumber($bin) { // print decimal equivalent of number print(bindec($bin)); }
Comments
Post a Comment