<?php
$debug=false;

if( $_SERVER['argc'] == 1 ) {

	$stdin = fopen('php://stdin', 'r');
	echo("Absolute path to infile: ");
	$infilename = trim(fgets($stdin));
	echo("Absolute path to outfile: ");
	$outfilename = trim(fgets($stdin));
} else if ( $_SERVER['argc'] == 3 ) {
	$infilename = $_SERVER['argv']['1'];
	$outfilename = $_SERVER['argv']['2'];
} else {
	echo("Usage: make-lower.php [ <infile> <outfile> ]\nWhere in and out file are absolute paths.\n");
	exit(2);
}

$ifid = fopen($infilename, 'r');
if( !$ifid ) {
	echo("Opening infile: $infilename failed.\n");
	exit(1);
}
$ofid = fopen($outfilename, 'w');
if( !$ofid ) {
	echo("Opening outfile: $outfilename failed.\n");
	exit(2);
}

$numlinebuf = 100000;	// how many lines to read at once? (tune according to PHP's memory constraints)
$maxtsnlen = 16;	// Max lenth of a given line (ignoring line ending!)
$separator = "\r\n";	// Line ending (typically \n or \r\n)
$linewidth = $maxtsnlen + sizeof($separator);
$buffsize = $linewidth * $numlinebuf;	// how much memory to fread/fwrite.

while( !feof($ifid) ) {
	$buffer = strtolower(fread( $ifid, $buffsize ));
	if( $debug ) {
		echo("\nBuffer contents:\n-----------------------\n");
		print_r( $buffer );
		echo("\n-----------------------\n");
	} else {
		fwrite( $ofid, $buffer );
	}
}

fclose($ifid);
fclose($ofid);
?>
