Wednesday 26 March 2014

File Handling Writing File in php

File handling writing file in php
File Handling Writing to the File In php Programming Languages. php file handling firstly opening the file in this article i am going to wirte data into file by php File handling. But you may read the data for this there is different attributes like w , r , a, (w)riting  ,  (R)eading , (A)ppend. So, here in this article we are going to use w for writing into the file by php file handling. For File firstly we will create handle which will open the file. Now you can see my localhost screenshot1 there is only one file.php is file which i am handling php file.

file handling writing file in php
Screenshot1

Code for File Handling Writing file in php (Screenshot2)

file handling writing file in php
Screenshot2


<?php



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



?>

Output (Screenshot3)

File Handling Writing File In php
Screenshot3
files in php
Screenshot4
File in php
Screenshot5
In this example we had $handle variable which contains fopen function to create the file even  it does not exit. So, First argument we can give is file name itself then we use commas and then second argument we give is whether w- write to the file , r - Read from the file or a - append to the file. In this example i want to write to the file so i am going to use w for writing to the file. and file i am going to called is names.txt. So, Its text file we can open up and we can view it as a text. Now If we execute this php Scripts or If we run this php script.

Screenshot3 On clicking file.php nothing will be the output (Screenshot4) but again returning to localhost in same directory you can see our fopen function php Scripts really works its Created file called name.txt. Screenshot4 and Screenshot5.

Now let's write in names.php by php Scripts.

Code For File Handling Writing File in php(Screenshot6)

File Handling in php
Screenshot6


<?php

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

fwrite($handle,'Alex');

?>

Output (Screenshot7)

File Handling Writing to file in php
Screenshot7
fwrite function php
Screenshot8
File Handling writing file in php
Screenshot9


Now In this example we are writing data to the file some data to the file. So, For writing to the file we had created handle for writing to the file we have to address to the file. fwrite  function i am passing to variables first to address where i am writing to therefore i will address to my file for that i had created variable $handle and using that variable to address my file to write in fwrite function and the another argument is the data i am writing to the file called alex. As you can see the output. I will not duplicate the file it will write data in same file names.txt. It will write alex to it. Screenshot7 i am running php scripts file.php for running the php script or to excecute the php scripts. Then in screenshot8 opening names.txt. You can see output in Screenshot9 names.txt.


Now For example If we change name Alex to Billy  because we still got this w - write here all is going to do is its going to it will write again. It will remove alex and will write billy if you want both name together to written in the file let see example.

Code For File Handling Writing File in php(Screenshot10)

fwrite function php
Screenshot10


<?php

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

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

fwrite($handle, 'Billy');

?>

Output (Screenshot11)

File Handling In php
Screenshot11
As you can see the output Alex and Billy i had used "\n" creating new line between alex and billy. So, we can write as many names in this list as we want.

So, This are the basic of writing to a file with  php and in other articles i will show you examples of reading and appending with php file . We can also use array with saving names or any other data to the file for example alex be an element of an array and billy be an element of an array

Code for File Handling Writing file in php(Screenshot12)

fwrite function in php
Screenshot12


<?php

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



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

fwrite($handle, 'Billy');



fclose($handle);



echo 'Written.php';



?>

Output (Screenshot15)

Screenshot15
file handling writing in file
Screenshot16
Important part of the file handling in php is to close the file which we had open by fopen function to close the file we can use fclose function to close the file in php. after writing the data to the file it is important to close the open file in php Scripts.You can see the output in Screenshots. 


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