PHP Tutorial (File Handling): Scratch Pad
Prerequisite
- Knowledge of basic PHP syntax.
In this tutorial you will learn about
- File Handling
Functions
We will be using 3 predefined functions for file handling
- fopen: To open a file
- file_get_content: To read a file
- 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:
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:
Closing a file
To close a file we use fclose() function.
Syntax:
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.
<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
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:
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.
We open the text.txt file in write mode and store the file descriptor in variable $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.
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
Code Explained
As you already read earlier, fopen() is used to open ‘text.txt’ in read mode.
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.
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:
Download:
http://rahulprasad.com/app/scratchpad/v0/cratchPadv0.zip
‹ Google Sidewiki : Annotate anything or everything Firefox tweak: Access forums without registering ›
-
Ujjwal Kanth
-
http://nishitasays.blogspot.com Radha
-
http://www.rahulprasad.com Rahul Prasad