Felix Felix - 5 months ago 20
PHP Question

PHP processing a large file

I've got a 1.5gig big file where I want to do some replacements.

But my apache runs out of memory. Is there a possibility to this job line by line?

<?php

$myfile = fopen("./m.sql", "r") or die("Unable to open file!");

$dateiinhalt = file_get_contents("./m.sql");


$search = array("ä",
"Ä",
"ö",
"Ö",
"ü",
"Ü",
"€",
"§",
"ß",
"latin1_general_ci",
"CHARSET=latin1",
"‚",
"„",
"‘",
"’",
"“",
"â€",
"©",
"®");

$replace = array("ä",
"Ä",
"ö",
"Ö",
"ü",
"Ü",
"€",
"§",
"ß",
"utf8_general_ci",
"CHARSET=utf8",
"‚",
"„",
"‘",
"’",
"“",
"”",
"©",
"®");


$content = str_replace($search, $replace, $dateiinhalt);
file_put_contents("./m.sql", $content);

echo "done";


thanks at all

Answer

Indeed. To adapt the example in the manual ( http://php.net/manual/en/function.fgets.php ):

<?php
$handleR = fopen("./m.sql", "r") or die('cannot open input file!');
$handleW = fopen("./m.out.sql", "w") or die('cannot open output file!');
if ($handleR) {
    // read one line or 4096 bytes, whichever comes first
    while (($dateiinhalt = fgets($handleR, 4096)) !== false) {
        // replace in that and write to output file
        fwrite($handleW, str_replace($search, $replace, $dateiinhalt));
    }
    if (!feof($handleR)) { // fgets() failed, but not at end of file
        echo "Error: unexpected fgets() fail\n";
    }
    fclose($handleR);
    fclose($handleW);
}
Comments