File_put_contents and file_get_contents exhaust memory size

I have a simple attempt at writing from one file to another.

$content=file_get_contents("C:\Users\Borut\Desktop\sql_PW1\mm_ads (1).sql");
file_put_contents("C:\Users\Borut\Desktop\sql_PW1\New", $content);

      

The file I am reading is about 80M, the memory limit in php is 128M, but I get the error:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 80739522 bytes)

      

So the memory is exhausted even though the memory I'm trying to allocate is actually less? I can make this work by increasing the memory limit, but I want to know why it doesn't work with that limit.

+3


source to share


4 answers


The amount it shows you in tried to allocate xxxx bytes

is the amount exceeding the PHP memory limit. This means that you ran out of your 128MB when you tried to allocate an extra ~ 80MB.

Even if you can fit the file into memory, when you know the file is going to be that big, you will be much better off using the fopen

/ fread

/ fwrite

/ combination fclose

.



I am assuming that you are going for more than just reading the content and writing it to another file, right? Because if that's all you want, you can just use the function copy

.

+5


source


First, let's say file_get_contents () allocates 80M of memory and file_put_contents () tries to allocate another 80M. You can try to put

var_dump(memory_get_usage())

      

between the lines.

note that this error message does not indicate how much memory is actually being used:



Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 80739522 bytes)

      

So this could mean PHP is actually using 100M and is trying to allocate another 80M

And of course, for such large files, you can use fopen () fwrite () and not immediately put the entire file into memory.

Also if you just want to copy these files you can just use copy ()

+1


source


Loading an entire file in PHP can exhaust your memory very well. If you need to work with a file that is too large for your memory, you will have to load it a little at a time, which means you cannot use file_get_contents()

.

But if all you are doing is copying the file, there is really no need to download it at all - PHP does provide copy()

function
. The work is done without the need to download it.

+1


source


To load large files without running out of memory, try the following function:

function custom_put_contents($source_url='',$local_path=''){

    $time_limit = ini_get('max_execution_time');
    $memory_limit = ini_get('memory_limit');

    set_time_limit(0);
    ini_set('memory_limit', '-1');      

    $remote_contents=file_get_contents($source_url);
    $response=file_put_contents($local_path, $remote_contents);

    set_time_limit($time_limit);
    ini_set('memory_limit', $memory_limit); 

    return $response;
}

      

Resources

Allowed memory size 134 217 728 bytes exhausted

+1


source







All Articles