Copy data from one file to another file

Write a PHP script that reads data from one file and write into another file.

Note: Before executing this program create a text document AboutPHP.txt

AboutPHP.txt

File handling is an important part of any web application. You often need to open and process a file for different tasks.

A better method to open files is with the fopen() function. fopen() contains the name of the file to be opened and the second parameter specifies in which mode the file should be opened.

The fread() function reads from an open file.

The first parameter of fread() contains the name of the file to read from and the second parameter specifies the maximum number of bytes to read.

The fwrite() function is used to write to a file.

The first parameter of fwrite() contains the name of the file to write to and the second parameter is the string to be written.

The fclose() function is used to close an open file.

The fclose() requires the name of the file (or a variable that holds the filename) we want to close.

Source Code: FileOperations.php
<?php
  $readFP = fopen("AboutPHP.txt", "r");
  $writeFP = fopen("PHPFiles.txt", "w");
  fwrite($writeFP,fread($readFP,filesize("AboutPHP.txt")));
  fclose($readFP);
  fclose($writeFP);
  $readFP = fopen("PHPFiles.txt", "r");
  echo fread($readFP,filesize("PHPFiles.txt"));
?>
Sample Output
File handling is an important part of any web application. You often need to open and process a file for different tasks.

A better method to open files is with the fopen() function. fopen() contains the name of the file to be opened and the second parameter specifies in which mode the file should be opened.

The fread() function reads from an open file.

The first parameter of fread() contains the name of the file to read from and the second parameter specifies the maximum number of bytes to read.

The fwrite() function is used to write to a file.

The first parameter of fwrite() contains the name of the file to write to and the second parameter is the string to be written.

The fclose() function is used to close an open file.

The fclose() requires the name of the file (or a variable that holds the filename) we want to close.