0

I have a file that has a lot of entries like this:

    18923081293801293.dateTime=12.10.2016 19\:46\:25
    18923081293801293.animal=cat
    18923081293801293.name=Fred
    18923081293801293.age=24
    67923781263804221.dateTime=15.04.2016 20\:26\:11
    67923781263804221.animal=horse
    67923781263804221.name=Larry
    67923781263804221.age=21
    ....

I am parsing the file...

 if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
    $file_path = $file['name'];
    $linesArray = file($file_path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
    $properties = array();


    foreach ( $linesArray as $str) {
        if (strlen($str) && $str[0] == '#') {
            $pdate = substr($str, 1);
            $date = rtrim($pdate);
            $formatted = DateTime::createFromFormat('* M d H:i:s T Y',$date);
        }
        rtrim ($str, "\n");
        $exp = explode ('=', $str);
        if(count($exp) == 2){
            $exp2 = explode('.', $exp[0]);  
            if( count($exp2) == 2 ) {
                if($exp2[1] == "dateTime"){
                    $s = str_replace("\\","",$exp[1]);
                    $d = strtotime($s);
                    $dateTime = date('Y-m-d H:i:s', $d);
                    $properties [$exp2[0]][$exp2[1]] = $dateTime;
                } else {
                    $properties [$exp2[0]][$exp2[1]] = $exp[1];
                }
            } else {
                $properties [$exp[0]] = $exp[1];
            }
        } 
    }
}

...and create an array:

var_dump($properties);

array(5) {
  ["18923081293801293"]=>
  array(5) {
    ["dateTime"]=>
    string(19) "2016-10-12 19:46:25"
    ["animal"]=>
    string(3) "cat"
    ["name"]=>
    string(4) "fred"
    ["age"]=>
    string(2) "24"
  }
.... and so on
}

This is usually working very well. But now I have a really large file with a lot of entries and the array is just not created. The result is a blank page. What can I do?

peace_love
  • 6,229
  • 11
  • 69
  • 157
  • 3
    Maybe you're running out of memory or the script is taking too long. Check your server error log. – Barmar Apr 26 '18 at 09:49
  • Why not use database though ? Your code would be much cleaner. Even single file based database, like SQL lite : https://www.sqlite.org/onefile.html – Aurelien Apr 26 '18 at 09:50
  • @Barmar On the top of my page I have "error_reporting(E_ALL);" but I do no see any error message on my page – peace_love Apr 26 '18 at 09:51
  • If the PHP script crashes, the error is reported in the log, not on the page. – Barmar Apr 26 '18 at 09:52
  • How large is your file/array? Some standard values for the PHP memory limits are about 50 Mb that is not that much for an large array. – xander Apr 26 '18 at 09:53
  • @xander The file is 200 MB but I already tested to change these values in my .htaccess file: `php_value memory_limit 1000M php_value upload_max_size 1000M php_value post_max_size 1000M php_value upload_max_filesize 1000M php_value max_execution_time 2000 php_value max_input_time 2000` – peace_love Apr 26 '18 at 09:55

0 Answers0