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


B2Evolution v6.7.9 Stable Blog PHP Script B2Evolution v6.7.9 Stable Blog PHP Script Create a beautiful blog website using this php scrpt. b2evolution is an advanced weblog tool, i-e soft...
Inventory and monitoring system PHP Script Inventory and monitoring system PHP Script PHP version requirement: at least 5.2 Features: 1. CRUD 2. Object Oriented Programming 3. AJ...
ImageHost v1.1.0 – Social Image Sharing PHP ... ImageHost v1.1.0 - Social Image Sharing PHP Script ImageHost is a social networking site based around sharing images! You can upload images and cre...
Chat System Using PHP/MySQL with PDO Query and Aja... Chat System Using PHP/MySQL with PDO Query and Ajax v2 PHP Script This Chat System will help you, specially to thus programmer who are building the...
eCommerce Fully functioned Online Shopping Site PH... eCommerce Fully functioned Online Shopping Site PHP Script Somstore is a fully functional eCommerce Online Sales system has it's own Dashboard desi...

Have any Question or Comment?

Leave a Reply

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

"