3

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

I am really tired, can someone help me?

EDIT: After dividing the file in 32 parts...

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):

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.

7
  • What does var_dump(34359738352, 34359738352 > 0); output on that system? Commented May 4, 2013 at 19:27
  • 1
    @hakre this: float(34359738352) bool(true) Commented May 4, 2013 at 19:30
  • Okay, then please report this as a bug. The error message you get by PHP is surely misleading (if not plain wrong). See Jon's answer, he hints a possible explanation (integer limit, I assume you are using a 32 bit operating system), but the error message should at least hint the type here as well or show the wrong number as well so it's clear about which value it moans. Commented May 4, 2013 at 19:33
  • @hakre: It's certainly very undesirable, but technically I believe there are grounds to shoot down a bug report on sight: php.net/manual/en/…. UB plus no warning at the same time! <3 PHP Commented May 4, 2013 at 19:37
  • @Jon: Many PHP functions use the float value if the integer space is not sufficient. The error message can be improved for sure anyway, and there is nothing wrong in giving feedback that can lead to improvements. Commented May 4, 2013 at 19:38

2 Answers 2

3

Looks like your file path is missing a backslash, which could be causing file_load_contents() to fail loading the file.

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.

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.

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)); 
}
Sign up to request clarification or add additional context in comments.

13 Comments

Now the error is PHP Fatal error: Allowed memory size of 1895825408 bytes exhausted (tried to al locate 2147483648 bytes) which I don't know how to fix that...
You can't read a file that large at once. Try instead iterating through it line-by-line.... will work on a solution for you.
I try dividing it into 32 parts... And nothing
Added info on how to process a file by buffering a little at a time.
Done.... now the example above will read the file in binary, 4 bytes at a time, and process each number as it reads it.
|
3

The last argument is an int, and apparently in your implementation of PHP int only has 31 bits for magnitude. 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?
  • 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?

7 Comments

@AlejandroMorais: I would say "manually fread up to the desired limit in chunks less than 2GB", but I 'm not sure if reading that much into memory at once is necessary and no better solution to your problem exists.
using "false" inthe second arguments, keeps throwing the error
@AlejandroMorais: Yes, that's not supposed to fix anything. It's just that you are being a bit liberal with the arguments, which is a bad habit to get into.
@AlejandroMorais: Your number needs 35 bits of magnitude to be represented as an integer, so you have to account for 35 - 31 = 4 bits. Which means up to 16 parts.
This is serious... "I AM GONNA TRY IT". Stay here for about 10 minutes while I finish writintg the code. I will then tell if I was succesful ;)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.