Php File Handling appending a file in php programming Language. File Handling Writing File we had seen to write into the file by php Scripts. Appending to the file is where we can add more to the existence file. Let see same example of File handling Writing file.
Code for File Handling Writing file in php(Screenshot1)
Screenshot1 |
<?php $handle = fopen('names.txt','w'); fwrite($handle,'Alex'."\n"); fwrite($handle, 'Billy'); fclose($handle); ?>
Output (Screenshot2)
Screenshot2 |
This script of php will create or open name names.txt write to names in it and close the file. Now if we again write another name it will erase or delete the existence names. So, If you want to the file as it is and want to add another name to it you can use appending to the file let see example.
Code for File Handling Appending a file (Screenshot3)
Screenshot3 |
<?php $handle = fopen('names.txt','a'); fwrite($handle,"\n".'Steven'."\n"); fclose($handle); ?>
Output (Screenshot4)
Screenshot4 |
Now i had change w to a for appending to the file in php programming Languages. It will Now add the name steven to our already created file names.txt with two another names alex, billy. If we use w it will overwrite over the file.
Code for File Handling Appending a file (Screenshot5)
Screenshot5 |
<?php $handle = fopen('names.txt','w'); fwrite($handle,"\n".'Steven'."\n"); fclose($handle); ?>
Output (Screenshot4)
Screenshot4 |
As you can see the output its override to the existence names. Now you can see the importance of the appending a file In php programming Language.
Now why do have writing to the file and Appending to the file features. We can use this for opening th file and writing once in it. then by appending feature we can reuse that file to.
Code for file Handling Appending a file
<?php if(isset($_POST['name'])) { $name = $_POST['name']; if(!empty($name)) { $handle = fopen('names.txt','a'); fwrite($handle, $name."\n"); fclose($handle); }else{ echo 'Please type a name.'; } } ?> <form action="file.php" method="POST"> Name:<br> <input type="text" name="name"><br><br> <input type="Submit" value="Submit"> </form>
Output (Screenshot)
Screenshot7 |
Screenshot8 |
In this example you had got idea how we can use this file handling by taking user data and save to the file. For Example here we had the names from the user and we are saving to the file names.txt. As you can see the output Screenshots.
Php File Handling Reading file
Php file handling reading file in php programming language. If we want to read a file want to read data from the file. If we want to read each name and display them to the user.
Code for Php File Handling Writing File
<?php if(isset($_POST['name'])) { $name = $_POST['name']; if(!empty($name)) { $handle = fopen('names.txt','a'); fwrite($handle, $name."\n"); fclose($handle); echo 'Current names in file:'; $readin = file('names.txt'); foreach($readin as $fname) { echo trim($fname). ','; }else{ echo 'Please type a name.'; } } ?> <form action="file.php" method="POST"> Name:<br> <input type="text" name="name"><br><br> <input type="Submit" value="Submit"> </form>
Output (Screenshot9)
Screenshot9 |
Screenshot10 |
In this example we are created a application for appending a file and display the file to the user. It will show the names. appended to the file. When the user will submit new name in the form as we seen in example of appending a file It will reproduce is the list of the current names. I will create list separated by commas not line by line so will use foreach loop.
$readin = file('names.txt');
Code will read the file and it will contains only file which we want to read file function stands for reading file in php programming language. When we use file function it will open and will read the file. Here we does not need to fopen function because file function will open and read file for us.
foreach($readin as $fname)
Code $readin is the array of names which are in file names.txt. Then we fetch all the names to fname.
echo fname.',';
Code will put commas after each names.
Now you can see current names in file. We also used trim function which will remove white space or any other special characters from our names.
No comments:
Post a Comment
Thanks For Comment Will get you Soon..