File Handling Concepts in PHP
Before going to file handling, you need to revise the Simple PHP programs, so click below this link to visit simple PHP programs,
1. To check that the sentence has Vowel, Alphabets and Words count using file concept
<html>
<head>
<title>File Handling I</title>
</head>
<body>
<center>
<?php
$file="ex1.txt";
$read=fopen($file,"a+");
$text=file_get_contents("ex1.txt");
$word = str_word_count($text);
$length = strlen($text);
$vowel = preg_replace("/[aeiou]/i", "", $text);
$alpha = preg_replace("/[abcdefghijklmnopqrstuvwxyz]/i","",$text);
$vowelcount=($length - strlen($vowel));
$alphacount=($length - strlen($alpha));
echo "<br>The vowels count in the passage is $vowelcount";
echo "<br>The words count is $word";
echo "<br>The alphabets count is $alphacount";
?>
</body>
</html>
Output:
For the above example, I already stored a file called 'ex1.txt' in wamp server.
So, that the php coding retrieves that data from that file and find the counts of the words,vowels and alphabets.You can download that file from link, which is given in the last of the post. And save it in the wamp server. If you store that file in other directory, output can't be executed.
2. Retrieve the Student's data from the file and show it in table form
<html>
<head>
<title>File Handling II</title>
</head>
<body>
<?php
if(file_exists('ex.txt'))
{
$f = fopen('ex.txt','r');
echo "<br><br>";
echo "<table border=2>";
echo "<tr><th>D. No.</th>";
echo "<th> Name</th>";
echo "<th> D.O.B</th>";
echo "<th> Age</th>";
echo "<th> Contact</th>";
echo "<th> E-mail ID</th></tr>";
while(!feof($f))
{
$d = fgets($f);
$s = explode(' ',$d);
if(!empty($s[0]) && !empty($s[1]) && !empty($s[2]) && !empty($s[3]) && !empty($s[4]) && !empty($s[5]))
echo "<tr><td>$s[0]</td>";
echo "<td> $s[1]</td>";
echo "<td> $s[2]</td>";
echo "<td> $s[3]</td>";
echo "<td> $s[4]</td>";
echo "<td> $s[5]</td></tr>";
}
echo "</table>";
}
?>
</body>
</html>
Output:
For this, I have already stored the student's data in the 'ex.txt' file. So, that according to the code it showed the student's details in tabular form.
3. Merge three files using File handling
<html>
<head>
<title>Merging Files</title>
</head>
<body>
<?php
$file1=fopen("hi.txt",'a+');
$file2=fopen("hello.txt",'a+');
$file3=fopen("he.txt",'a+');
$merge=fopen("merge.txt","w+");
$m=file_get_contents("hi.txt");
fwrite($merge,$m);
$n=file_get_contents("hello.txt");
fwrite($merge,$n);
$o=file_get_contents("he.txt");
fwrite($merge,$o);
echo "That three files are merged with the First file \"merge.txt\"";
?>
</body>
</html>
Output
For this, I have stored the file named, 'hi','hello' and 'he' .txt files. I created the 4th file called 'merge', when I calling this, it is automatically created. In this, all the three files content will be merged.
You can get those all text files by clicking this given below link
Download this file save it in Local Disk C --> wamp64 --> www --> and save your text file. Otherwise, the mentioned above codes are not working. If you don't download this, you will need to create new files in that location and named like the mentioned above files. Or change files name in the 'fopen' function.
If you have any doubt on this, post your comment on comment box which is in menu bar.
Comments
Post a Comment