出现这个错误消息,有什么建议吗?

允许内存大小为33554432字节已耗尽(已尝试分配 43148176字节)的PHP


当前回答

ini_set (memory_limit’,‘- 1);

在你的index.php中,在PHP标签打开后的顶部

其他回答

如果你想读取大文件,你应该一点一点地读取,而不是一次读取。 这是一个简单的数学:如果您一次读取一个1mb大的文件,那么同时至少需要1mb的内存来保存数据。

所以你应该使用fopen & fread一点一点地阅读它们。

做的事情:

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

从来都不是好事。如果你想读取一个很大的文件,最好的做法是一点一点地复制它。尝试以下代码进行最佳实践。

$path = 'path_to_file_.txt';

$file = fopen($path, 'r');
$len = 1024; // 1MB is reasonable for me. You can choose anything though, but do not make it too big
$output = fread( $file, $len );

while (!feof($file)) {
    $output .= fread( $file, $len );
}

fclose($file);

echo 'Output is: ' . $output;

Wordpress用户添加一行:

@ini_set('memory_limit', '-1');

or

在cpanel中设置memory_limit为-1

我运行以下命令编写器更新,现在正在工作。如果它不能解决你的问题,增加你的内存限制在memory_limit在php.ini文件。

It is unfortunately easy to program in PHP in a way that consumes memory faster than you realise. Copying strings, arrays and objects instead of using references will do it, though PHP 5 is supposed to do this more automatically than in PHP 4. But dealing with your data set in entirety over several steps is also wasteful compared to processing the smallest logical unit at a time. The classic example is working with large resultsets from a database: most programmers fetch the entire resultset into an array and then loop over it one or more times with foreach(). It is much more memory efficient to use a while() loop to fetch and process one row at a time. The same thing applies to processing a file.