Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Firstly you should understand that when using file_get_contents you're fetching the entire string of data into a <em>variable</em>, that <em>variable</em> is stored in the hosts memory.</p> <p>if that string is greater then the size dedicated to the PHP process then PHP will halt and display the error message above.</p> <p>The way around this to open the file as a pointer, and then take a chunk at a time, this way if you had a 500MB File you can read the first 1MB of data, do what you will with it, delete that 1MB from the system's memory and replace withthe next MB, This allows you to manage how much data your putting in the memory.</p> <p>An example if this can be seen below, I will create a function that acts like to node.js</p> <pre><code>function file_get_contents_chunked($file,$chunk_size,$callback) { try { $handle = fopen($file, "r"); $i = 0; while (!feof($handle)) { call_user_func_array($callback,array(fread($handle,$chunk_size),&amp;$handle,$i)); $i++; } fclose($handle); } catch(Exception $e) { trigger_error("file_get_contents_chunked::" . $e-&gt;getMessage(),E_USER_NOTICE); return false; } return true; } </code></pre> <p>and then use like so:</p> <pre><code>$success = file_get_contents_chunked("my/large/file",4096,function($chunk,&amp;$handle,$iteration){ /* * Do what you will with the {&amp;chunk} here * {$handle} is passed in case you want to seek ** to different parts of the file * {$iteration} is the section fo the file that has been read so * ($i * 4096) is your current offset within the file. */ }); if(!$success) { //It Failed } </code></pre> <p>One of the problems you will find is that your trying to perform regex several times on an extremely large chunk of data, not only that but your regex is built for matching the entire file.</p> <p>With the above method your regex could become useless as you may only be matching a half set of data, what you should do is revert to the native string functions such as </p> <ul> <li><code>strpos</code></li> <li><code>substr</code></li> <li><code>trim</code></li> <li><code>explode</code></li> </ul> <p>for matching the strings, i have added support in the callback so that the handle and current iteration are passed, this will allow you to work with the file directly within your callback, allowing you to use functions like <code>fseek</code>, <code>ftruncate</code> and <code>fwrite</code> for instance.</p> <p>The way your building your string manipulation is not efficient what so ever, and using the proposed method above is by far a much better way.</p> <p>Hope this helps.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload