Wednesday 26 March 2014

File handling appending a file in php

file operation in php
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)

writing to file in php
Screenshot1


<?php

$handle = fopen('names.txt','w');



fwrite($handle,'Alex'."\n");

fwrite($handle, 'Billy');



fclose($handle);
?>

Output (Screenshot2)

file handling in php
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)

appending file in php
Screenshot3


<?php

$handle = fopen('names.txt','a');



fwrite($handle,"\n".'Steven'."\n");



fclose($handle);

?>

Output (Screenshot4)

appending file in php
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)

file handling appending a file in php
Screenshot5


<?php

$handle = fopen('names.txt','w');



fwrite($handle,"\n".'Steven'."\n");



fclose($handle);

?>

Output (Screenshot4)

appending file in php
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)

appending file on php
Screenshot7

appending file on php
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)

file handling in php
Screenshot9
php file handling appending reading file in php
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..

About Me

Welcome to Extra Tutorials! My name is Mohammed and I am the 22 year writer, website developer, and photographer behind the blog. Thanks for visiting! Tutorials Jackpot… In addition to Developer, I love to develop websites and I love to write. Starting a php Blog was inevitable for me. What began as a simple way to share all of my Tutorials with friends and family has developed into my Part time job.

Mohammed Padela

WHAT IS PHP PROGRAMMING

WHAT IS PHP PROGRAMMING
WHAT IS PHP PROGRAMMING

Follow Us

Popular Posts

Designed ByBlogger Templates