Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么在运行php脚本时总是出现内存分配错误?_Php - Fatal编程技术网

为什么在运行php脚本时总是出现内存分配错误?

为什么在运行php脚本时总是出现内存分配错误?,php,Php,我正在迭代一些csv文件(没有一个超过320MB),以使用以下脚本清理和格式化文件,并按照我需要的方式对其进行格式化: while($row=mysqli\u fetch\u数组($rsSelect)){ $fileName=$row[“文件名”]; if(strpos($fileName,'DATA')==true){ $file=$dir.$row[“清单文件”]。“”; echobr($文件); $file1=file\u get\u contents($file,null); 未设置($

我正在迭代一些csv文件(没有一个超过320MB),以使用以下脚本清理和格式化文件,并按照我需要的方式对其进行格式化:

while($row=mysqli\u fetch\u数组($rsSelect)){
$fileName=$row[“文件名”];
if(strpos($fileName,'DATA')==true){
$file=$dir.$row[“清单文件”]。“”;
echobr($文件);
$file1=file\u get\u contents($file,null);
未设置($文件);
$file2=str_replace(“,”,“,”$file1);
未结算(1美元);
$file3=str_replace(“,”$file2);
文件内容($dir.$row[“file\u name”]。.txt“,$file3,文件使用\u包含\u路径);
未结算(2美元);
未结算(3美元);
}
无论我在php.ini中设置了多高的内存限制,或者在可以释放内存的地方取消了变量设置,我都会收到以下错误。我也尝试在脚本中设置限制,但仍然没有成功。我的机器不缺少RAM,当我需要文件时,可以很容易地进行存储

错误:(!)致命错误:内存不足(已分配683147264)(试图分配364322204字节)


使用
file\u get\u contents()
需要将整个文件以一个大数据块的形式读入内存。您最好使用
fopen()
并在
fgets()
周围循环。这样一次只能读取一行:

$fp = fopen('file.csv', 'r');
while (($line = fgets($fp)) !== false) {
    // $line is the whole line
}
或者,您可以使用
fgetcsv()
并根据需要处理各个字段:

$fp = fopen('file.csv', 'r');
while (($row = fgetcsv($fp)) !== false) {
    // $row is an array of values from the line
}

使用
file\u get\u contents()
需要将整个文件以一个大数据块的形式读入内存。您最好使用
fopen()
并在
fgets()
周围循环。这样一次只能读取一行:

$fp = fopen('file.csv', 'r');
while (($line = fgets($fp)) !== false) {
    // $line is the whole line
}
或者,您可以使用
fgetcsv()
并根据需要处理各个字段:

$fp = fopen('file.csv', 'r');
while (($row = fgetcsv($fp)) !== false) {
    // $row is an array of values from the line
}

上次
unset
append
gc\u collect\u cycles();
after last
unset
append
gc\u collect\u cycles();
Ah,解释得很好!感谢您的回复Alex HowaskyAh,解释得很好!感谢您的回复Alex Howasky