Rahul's Tech Blog

Developer Zone

PHP Tutorial (File Handling): Scratch Pad


Prerequisite

  1. Knowledge of basic PHP syntax.

In this tutorial you will learn about

  1. File Handling

Functions

We will be using 3 predefined functions for file handling

  1. fopen: To open a file
  2. file_get_content: To read a file
  3. fclose: To close a file

Little theory before we begin

Opening a file

Predefined function fopen() is used to open a file. It returns a variable which can be used for accessing the file. Returned variable is called as file handler.
Syntax:

$var = fopen("path to file", "mode");
 

mode: It controls how you want to access a file.
Note: It opens the file only if the logged-in user has permission to open it. If unable to open the file it returns false.

Read more about fopen

Reading a file

To read a file at once we use the function file_get_contents(). It returns a string containing everything in file.
Syntax:

$var = file_get_contents("path to file");
 
Closing a file

To close a file we use fclose() function.
Syntax:

fclose($filehandler);
 

PROJECT

We will be creating a scratch pad, where you can write/update any text.

Step 1: User Interface

First we need to create the user interface i.e. a form where we can write/edit text.
Here is the user interface that I have created.
Here is the code.

<form id="form1" action="back_end.php" method="post">
            <textarea id="data" cols="45" rows="5" name="data"></textarea>
<input id="save" name="save" type="submit" value="Save" />
</form>
 

Step 2: Back End

Our application’s interface is “interface.php”. We will send the data from interface.php to back_end.php which will processes the data and redirect back to interface.php to shows updated page.

back_end.php

Here is the code for back-end.php

<?php
if(isset($_POST)) {
        $data = $_POST[‘data’];
        $fd = fopen(‘text.txt’,‘w’);
        if($fd > 0)     {
                fwrite($fd, $data);
                fclose($fd);
        }
}
header("Location: interface.php");
?>
Code explained:
if(isset($_POST)) {
        $data = $_POST[‘data’];

First we check is there anything sent using POST method, to this page. This is used to prevent emptying of “text.txt” if back_end.php is directly opened. If something is sent it is stored in a variable $data.

$fd = fopen(‘text.txt’,‘w’)

We open the text.txt file in write mode and store the file descriptor in variable $fd.

fwrite($fd, $data);
fclose($fd);

Then we write to the file using fwrite() function. We need to supply the file descriptor ($fd) and data ($data) to the function.
After that we close the function using fclose() function.

header("Location: interface.php?saved=1");
 

After received data is written to text.txt it must be redirected back to the interface.php where updated text can be displayed. We send a GET variable ‘saved=1′.

interface.php

Now we will add code to interface.php to read from text.txt. Code should be added at the top of document to keep it separate from HTML document.
You can also write PHP code in a separate document and include it in current document using include(), require(), include_once() and require_once() functions.
Here is the code for interface.php

$fd = fopen(‘text.txt’,‘r’);
$data = fread($fd, filesize(‘text.txt’));
fclose($fd);
 
Code Explained

As you already read earlier, fopen() is used to open ‘text.txt’ in read mode.

$data = fread($fd, filesize(‘text.txt’));
 

fread() function is used to open file. We need to provide the file descriptor as first argument $fd and size of the file as second argument.

$size = filesize(‘text.txt’);
 

Function filesize() is used to obtain the size of file.
The read data is stored in $data variable.
The we close the file using fclose() function.

Now we need to print $data variable in between >textarea< >/textarea< tag to load it in text-area box.
Code:

<textarea id="data" cols="45" rows="5" name="data"><?php echo $data; ?></textarea>
 

Download:

http://rahulprasad.com/app/scratchpad/v0/cratchPadv0.zip

:)

, , ,

  • Ujjwal Kanth

    Keep up the good work ..

  • http://nishitasays.blogspot.com Radha

    Good work buddy, U seem really a good teacher…
    Good Luck

  • http://www.rahulprasad.com Rahul Prasad

    @Radha: Thank you. You may subscribe to site’s RSS feed.