append contents of first file into second file | PHP - IProgramX

Q. Write a program to read two file names from user and append contents of first file into second file. 


HTML File:

<html>
<body>
<form action="a3a1.php" method="POST">
Enter First File:<input type="text" name="first"><br><br>
Enter Second File:<input type="text" name="second"><br><br>
<input type="submit">
</form>
</body>

</html>

PHP File:
NOTE: PHP function is saved as "a3a1.php"
<?php
$first=$_POST['first'];
$second=$_POST['second'];
if(!file_exists($first))
{
die("<br>$first does not exist");
}
if(!file_exists($second))
{
die("<br>$second does not exist");
}
$fp1=fopen($first,'r') or die("unable to open first file");
$fp2=fopen($second,'a') or die("unable to open second file");
$data=fread($fp1,filesize($first));
fwrite($fp2,$data);
fclose($fp1);
fclose($fp2);
echo "<h2>Contents of first file is appanded to second file</h2>";
?>

NOTE: First create two files and also write something in the 1st file and 2nd file become empty then run the script

Output:



Post a Comment

0 Comments