出现这个错误消息,有什么建议吗?
允许内存大小为33554432字节已耗尽(已尝试分配 43148176字节)的PHP
出现这个错误消息,有什么建议吗?
允许内存大小为33554432字节已耗尽(已尝试分配 43148176字节)的PHP
当前回答
我们也遇到过类似的情况,我们尝试了顶部给出的答案 报错(' memory_limit ', ' 1 '); 一切工作正常,压缩图像文件大于1MB到KBs。
其他回答
这里有两个简单的方法来增加共享主机的限制:
如果您可以访问PHP.ini文件,请更改PHP.ini中的行 如果你的线显示为32M,试试64M: memory_limit = 64M;一个脚本可能消耗的最大内存(64MB) 如果你不能访问PHP.ini,试着把这个添加到。htaccess文件中: php_value memory_limit 64M
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.
写
ini_set (memory_limit’,‘- 1);
在你的index.php中,在PHP标签打开后的顶部
做的事情:
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;
如果您试图读取一个文件,这将占用PHP中的内存。例如,如果您试图打开并读取一个MP3文件(例如,$data = file("http://mydomain.com/path/sample.mp3")),它将把它全部拉到内存中。
正如Nelson所建议的,如果您确实需要使用这么多内存,您可以提高您的最大内存限制。