In theory, converting this code to PHP should be faster, as CGI requires an external process to start up and run. Again,
in theory.
As for the actual algorithm, it may be a bit more efficient to read in the entire file and call the print function once for each buffer. That will cut out all the function call overhead, which is called hundreds of times currently.