title: unpack(C*,data) Eats Memory [php exhausting memory on unpack function]
body: <p>I want to read a binary file as byte[] in php , as suggested <a href="https://stackoverflow.com/questions/885597/string-to-byte-array-in-php">here</a> i <a href="http://www.php.net/manual/en/function.unpack.php" rel="nofollow noreferrer"><code>unpack</code></a>'ed fread's output . so i have something like:</p>
  
  <pre><code>$file=fopen($filename,'r');
  fseek($file, $offset);  //file is 500MB so i take it 10MB at a time
  $tmp = fread($file,$len);
  //so far so good , $tmp includes 10MB of data
  var_dump(strlen($tmp));    //int(10485760) 10MB
  var_dump(memory_get_usage(true)); //int(11272192) 11MB
  $data = unpack('C*',$tmp);
  </code></pre>
  
  <p>this throws </p>
  
  <pre><code>PHP Fatal error:  Allowed memory size of 536870912 bytes exhausted (tried to allocate 32 bytes) in [myfile.php] on line [unpack line]
  </code></pre>
  
  <p>as the error suggests memory limit is set at 512MB and according to <a href="http://www.php.net/manual/en/function.memory-get-usage.php" rel="nofollow noreferrer"><code>memory_get_usage</code></a> only 11/512MB was used and i am unpacking a 10MB string . at most it should need 30MB (10MB for $tmp , 10MB for $data and 10MB for internal variables). why does it explode and can't unpack $tmp with 512MB ram ?</p>
  
  <p><strong>so the question is</strong> , am i doing something wrong here or is it a bug? and is there any other way to get an array of Bytes (0 to 255) to read binary files in php or should i switch to another language to do this?</p>
  
  <p>additional notes : the code works with a 117KB file.</p>
  
  <pre><code>php -v
  PHP 5.5.3-1ubuntu2.2 (cli) (built: Feb 28 2014 20:06:05) 
  Copyright (c) 1997-2013 The PHP Group
  Zend Engine v2.5.0, Copyright (c) 1998-2013 Zend Technologies
      with Zend OPcache v7.0.3-dev, Copyright (c) 1999-2013, by Zend Technologies
  </code></pre>