Saturday, 28 September 2013

PHP using iconv to convert large files

PHP using iconv to convert large files

I need to convert text files' character encodings without hogging the
server's memory, while the input file is user configured and its size
isn't limited.
Would it be more efficient to wrap an unix's iconv command using exec()
(which I'd rather avoid, although I already use it in the application for
other file operations), or should I read the file line by line and output
it into another file?
I'm thinking working this way:
$in = fopen("in.txt", "r");
$out = fopen("out.txt", "w+");
while(($line = fgets($in, 4096)) !== false) {
$converted = iconv($charset["in"], $charset["out"], $line);
fwrite($out, $converted);
}
rename("out.txt", "in.txt");
Is there any better approach to convert the file fast and efficiently? I'm
thinking this might be rather CPU intensive, but then I believe iconv
itself is an expensive task so I'm not sure if I can make it actually not
eat the server much at all.
Thanks!

No comments:

Post a Comment