file_put_contents和file_get_contents耗尽内存大小 [英] file_put_contents and file_get_contents exhaust memory size
问题描述
我有一些简单的尝试,可以从一个文件写入另一个文件.
I have some simple trying to write 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);
我读取的文件大约有80M,php中的内存限制为128M,但出现错误:
The file which I read is about 80M big, the memory limit in php is 128M, but I get an 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 am trying to allocate is actually smaller?? I can make this work by increasing the memory limit, but I want to know why it doesnt work with this limit.
推荐答案
在试图分配xxxx字节
中显示的数量超出了PHP中的内存限制.这意味着您在尝试分配额外的〜80MB时已经用完了128MB.
The amount that it shows you in tried to allocate xxxx bytes
is the amount over and above the memory limit in PHP. This means you have exhausted your 128MB while you were trying to allocate an additional ~80MB.
即使您可以将文件放入内存中,当您知道文件太大时,结合使用 fopen
/fread
/ fwrite
/ fclose
.
Even if you can fit the file into memory, when you know the file is going to be that large, it will be a lot better for you to use a combination of fopen
/fread
/fwrite
/fclose
.
我认为您要做的不只是读取内容并将其写入另一个文件,对吗?因为只要您需要,您就可以使用 copy
函数.
I assume that you're going more than just reading the contents and writing it to another file, though, right? Because if that's all you need, you can just use the copy
function.
这篇关于file_put_contents和file_get_contents耗尽内存大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!