codeigniter Php内存错误,但我已经调整我的php.ini在CI4

b1zrtrql  于 2022-12-07  发布在  PHP
关注(0)|答案(1)|浏览(102)

我使用的是PHP7.4和Ci4.19
我正在阅读的文件小于14 MB。我得到以下两个错误;

Fatal error: Allowed memory size of 157286400 bytes exhausted (tried to allocate 20480 bytes) in /var/www/example.com/development.example.com/app_dir/vendor/codeigniter4/framework/system/Common.php on line 395

和这个;

Fatal error: Allowed memory size of 157286400 bytes exhausted (tried to allocate 20480 bytes) in /var/www/example.com/development.example.com/app_dir/vendor/codeigniter4/framework/system/Debug/Exceptions.php on line 154

在我的php.ini文件中,我有以下设置,是的,我在更改设置后重新启动了apache 2;

memory_limit = 150M
post_max_size = 150M

不知道我得到这个错误和如何修复它。
她是代号;

public function ski_index()
{

    helper("filesystem");
                  
    // Read the JSON file      
    $json = file_get_contents(WRITEPATH.'/ski/raw_data/ski_areas.geojson');

    // Decode the JSON file to an array
    $array_data = json_decode($json,true);
     
    // for the view
    $this->data['array_data'] = $array_data;       

    // $new_array will collected filtered ski resorts by country
    $new_array = array();

    $j=1;
    foreach ($array_data['features'] as $value){
        // if isset and filter by country
        if (isset($value['properties']['location']['iso3166_1Alpha2']) && $value['properties']['location']['iso3166_1Alpha2'] == 'JP'){
            array_push($new_array, $value);
            $j++;
        }
    }
    
    // reconstruct by putting the array into FeatureCollection
    $reassemble_data = array ('type'=>'FeatureCollection', 'features'=>$new_array);
    
    // convert it back to json/geojson
    $this->data['new_array'] = json_encode($reassemble_data);     

    //write out to new file
    $file_content = $this->data['new_array'];
    if (!write_file(WRITEPATH."/ski/filtered_data/japan_ski_resorts.geojson", $file_content)){
        echo "Error - cannot write to path";
    } else {
        echo "Success - written to file";
    }
    return view('/Admin/Ski/ski_index', $this->data );
}
nxowjjhe

nxowjjhe1#

默认情况下,PHP变量通过值传递。如果创建变量来存储值,则会有该数据的克隆,这需要更多内存。节省内存的一种方法是避免创建大量变量来存储相同的数据。例如:

$this->data['array_data'] = $array_data;   // Used only in view, avoid

请参阅下面的代码,了解如何使用更少的不需要的变量。

public function ski_index()
{

helper("filesystem");
              
// Read the JSON file      
$json = file_get_contents(WRITEPATH.'/ski/raw_data/ski_areas.geojson');

// Decode the JSON file to an array
$this->data['array_data'] = json_decode($json,true);
$json = ''; // clearing memory
 
      

// $new_array will collected filtered ski resorts by country
$new_array = array();

$j=1;
foreach ($this->data['array_data']['features'] as $value){
    // if isset and filter by country
    if (isset($value['properties']['location']['iso3166_1Alpha2']) && $value['properties']['location']['iso3166_1Alpha2'] == 'JP'){
        array_push($new_array, $value);
        $j++;
    }
}

// reconstruct by putting the array into FeatureCollection & convert it back to json/geojson
$this->data['new_array'] = json_encode(array ('type'=>'FeatureCollection', 'features'=>$new_array));     

//write out to new file
if (!write_file(WRITEPATH."/ski/filtered_data/japan_ski_resorts.geojson", $this->data['new_array'])){
    echo "Error - cannot write to path";
} else {
    echo "Success - written to file";
}

// for the view

return view('/Admin/Ski/ski_index', $this->data );
}

相关问题