This question already has an answer here:
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
Updated Code:
$reading = fopen('m.sql', 'r');
$writing = fopen('m.tmp', 'w');
$replaced = false;
$search = array("ä",
"Ä",
"ö",
"Ö",
"ü",
"Ü",
"€",
"§",
"ß",
"latin1_general_ci",
"CHARSET=latin1",
"‚",
"„",
"‘",
"’",
"“",
"â€",
"©",
"®");
$replace = array("ä",
"Ä",
"ö",
"Ö",
"ü",
"Ü",
"€",
"§",
"ß",
"utf8_general_ci",
"CHARSET=utf8",
"‚",
"„",
"‘",
"’",
"“",
"”",
"©",
"®");
if ($reading) {
// read one line or 4096 bytes, whichever comes first
while (($dateiinhalt = fgets($reading, 4096)) !== false) {
// replace in that and write to output file
fwrite($writing, str_replace($search, $replace, $dateiinhalt));
}
if (!feof($reading)) { // fgets() failed, but not at end of file
echo "Error: unexpected fgets() fail
";
}
fclose($reading);
fclose($writing);
}
echo "done";
</div>
Yes. Its not hard. You seem to have already started - you should not open a file before running file_get_contents() on it (unless you explicitly want to lock it).
$search=Array(...
$replace=Array(...
$myfile = fopen("./m.sql", "r");
$output = fopen("./output.sql", "w");
while ($line=fgets($myfile)) {
fputs($output, str_replace($search, $replace, $line));
}
Instead of file_get_contents
(which reads the entire file into memory at once), use fopen
and fgets
to stream the file line-by-line.
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
";
}
fclose($handleR);
fclose($handleW);
}