Home Php PHP File Manipulation Writing to a File Tutorial
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

PHP File Manipulation Writing to a File Tutorial

Advertisement
Find out how to write data to a flat file database (text document) using this tutorial.

File Manipulation in PHP

     

In this tutorial we will learn how to write data to a flat file database (text document) using this tutorial. 

In PHP, we can do many things with file, like creation of file, update of file, deletion of file etc. One of the important work  is writing to a file, following example will illustrate how to open a file and write into it, in addition we will learn how to open a file and read the The following examples will let you know about the most basic form of file handling.

Example 1 (Let's assume that we have created a .txt file inside C:\wamp\www\php directory, where we will write the .php file):

<?php

$file=fopen("Newest.txt","r");

while(!feof($file))

{

  echo (fgets($file));

}

?>

Output:

In PHP, we can do many things with file, like creation of file, update of file, deletion of file etc. One of the important work is writing to a file, following example will illustrate how to open a file and write into it. In addition we will learn how to open a file and read the The following examples will let you know about the most basic form of file handling.

Example 2 (If you want to create the file, we will use 'w' mode instead of 'r'):

<?php

$file=fopen("Newest.txt","w");

$str="This is same file with different text";

fputs($file,$str);

$file=fopen("Newest.txt","r");

while(!feof($file))

{

  echo fgets($file)."<br/>";

}

?>

Output:

This is same file with different text

Example 3 (If you want to append some text into a file change the mode into  'a'):

<?php

$file=fopen("Newest.txt","a");

$str="This is same file with extra text which has been appended";

fputs($file,$str);

$file=fopen("Newest.txt","r");

while(!feof($file))

{

  echo fgets($file)."<br/>";

}

?>

Output:

This is same file with different textThis is same file with extra text which has been appended

Note: For further details please visit our web page: http://roseindia.net/tutorial/php/phpbasics/tutorial/PHP-File-Handling.html


 
Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 9, 2009

Related Tutorials

Ask Questions?    Discuss: PHP File Manipulation Writing to a File Tutorial  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Ryne
June 15, 2013
vlGhaXdyWDPMbGIBl

A really good asnewr, full of rationality!
amit kumar
February 11, 2013
PHP QUESTIONS

dear sir, i want to ask a question. how we can upload or attach multiple images at a time and store in a mysql database in single field. please help me.
malik
November 19, 2011
file

how can I save data into a pdf file ? like if I had a web page for creating cv for users how can I save data entered in text boxes into a pdf file
balu
December 30, 2011
php doubt

how to display the middle lines of the file after uploading using php html code into database and display the middle lines in html form from database
DMCA.com