Google Ads

Handle File v1 PHP Script


Handle File v1 PHP Script

In this tutorial you will be learn how to read/ write / append files using php. There are some situations you don’t need store all the information inside databases. Most cases databases applications are not 100% satisfy with the software requirement to maintaining data. Situation like maintaining error log configuration files etc.. Maintaining file are the oldest method of storing data in computer systems. Before start operation, we have to open required file with required permission and also after operation on file we should closed the file.
Following php code shows you how to open files with write permission

 

 
 
  1. $testFile = "testFile.txt";
  2. $file_handler = fopen($testFile 'w');

 

Following php code shows how to open file with read permission

 

 
 
  1. $testFile = "testFile.txt";
  2. $file_handler = fopen($testFile 'r');

 

Following php code shows how to open files with append permission

 

 
 
  1. $testFile = "testFile.txt";
  2. $file_handler = fopen($testFile 'a');

 

 

You need better understand about file opening mode before do any file operation. There are number of file opening modes available as follows

r – Open files for read mode (file pointer start at the beginning of the file)
w – Open files for read mode ( file pointer start at the beginning of the file if you write something, existing data will be erase. also this function will create new file if file is not exist )
a – Open the file with pointer at end of the file so existing data will be available and writing start at end of file
x – Create file for write only. If file is not exist this will return an error
r+ – Open the file pointer start at beginning of the file for read/write
w+ – Open the file pointer start at beginning of the file , erase the content of the file. If file is not exist, will create new file
a+ – Open the file pointer at end of file so no harm to existing content. This will create new file if the file is not exist
x+ – Create new file for read/ write and return false if file is exist

Then we need to know how to close the file. This is very important because resource used by file pointer remain in memory if we will not close the file

 

 
 
  1. fclose($file_handler);

 

So far you learn how to open file with modes and how to close the file. Now I am going to teach you how to do read/ write/ append operation on file

For writing we are using fwrite() function. Usage of file writing function as follows

 

 
 
  1. $testFile = "testFile.txt";
  2. $file_handler = fopen($testFile 'w') or die("Unable to open file!");
  3. $line = "This is my first writing\n";
  4. fwrite($file_handler, $line);
  5. $line2 = "This is my second writing\n";
  6. fwrite($file_handler, $line2);
  7. fclose($file_handler);

 

 

Then we are going to learn how to read file content. To read file content we are using fget() function which is return one line of from the file. Also you need to know about feof() function which is checking file has reach the end of content

 

 
 
  1. $testFile = "testFile.txt";
  2. $file_handler = fopen($testFile 'r') or die("Unable to open file!");
  3. while(!feof($file_handler)) {
  4. $file_line = fgets($file_handler) ;
  5. echo $file_line.” <br>”;
  6. }
  7. fclose($file_handler);

 

 

Following php code shows how to append file with

 

 
 
  1. $testFile = "testFile.txt";
  2. $file_handler = fopen($testFile, a') or die("Unable to open file!");
  3. $line = "This is my third writing\n";
  4. fwrite($file_handler, $line);
  5. $line2 = "This is my forth writing\n";
  6. fwrite($file_handler, $line2);
  7. fclose($file_handler);

 

 

sample code contain simple shopping cart coded using file manipulation

 

 

 


Related Post

Ajax Contact Form with attachments 2.0 PHP Script ... Ajax Contact Form with attachments 2.0 Ajax Contact Form with attachments 2.0 Ajax Contact Form with Attachments and no spam control (drag and drop, r...
Tanks Battle Field – HTML 5 Game PHP Script ... Tanks Battle Field - HTML 5 GameTanks Battle Field - HTML 5 Game“Tanks Battle Field” is unique game featuring the classic “capture the flag” g...
Bookingo – Hotel Booking System PHP Script Downl... Bookingo – Hotel Booking SystemBookingo – Hotel Booking SystemBookingo Hotel Booking System – is the perfect platforms to manage reservations, r...
Classic Bowling – HTML5 Sport Game PHP Scrip... Classic Bowling - HTML5 Sport GameClassic Bowling - HTML5 Sport GameClassic Bowling is a HTML5 Sport Game. Throw the ball and hit as many pins as you ...
MelonHTML5 – Metro UI PHP Script Download MelonHTML5 - Metro UI MelonHTML5 - Metro UI Metro UI is a flexible and easy to integrate framework to build your one page website in Windows 8 Metro U...

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *

"