Allowed memory size of 33554432 bytes exhausted (tried to allocate 43148176 bytes) in php

前端 未结 21 1236
猫巷女王i
猫巷女王i 2020-11-21 22:14

This error message is being presented, any suggestions?

Allowed memory size of 33554432 bytes exhausted (tried to allocate 43148176 bytes) in php

21条回答
  •  太阳男子
    2020-11-21 23:05

    Doing :

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

    is never good. If you want to read a very large file, it is a best practise to copy it bit by bit. Try the following code for best practise.

    $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;
    

提交回复
热议问题