PHP File Processing: Read and Write

Reading from a file and writing to a file in PHP. The code snippet below reads the file line by line and then writes each line read to a new file. The end results is to make a copy of the file it reads.

$file_r = "myfile.txt";
$file_w = "myfile_copy_".date("YmdHis").".txt";
if (file_exists($file_r) && is_readable ($file_r)) {
	$file_r_handler = fopen($file_r, "r");
	$file_w_handler = fopen($file_w, "w");
	while (!feof($file_r_handler)) {
		$line = fgets($file_r_handler);
		fwrite($file_w_handler, $line);
		echo $line;

Search within Codexpedia

Custom Search

Search the entire web

Custom Search