How to Handle File With PHP

Submitted by: 
Language: 
Visitors have accessed this post 1448 times.

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.  
  8. 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.  
  8. fclose($file_handler);

sample code contain simple shopping cart coded using file manipulation


Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.