Home Tutorials IT Jobs Source Codes Certifications Discussion Forum
  PHP Tutorials
PHP Introduction
PHP Installation
Syntax
Variables
Echo
Strings
Operators
Comments
Require
Include
If...Else
Switch
Forms
Functions
Arrays
While Loop
For Loop
For Each
Do While
Post & Get
File Create
File Open
File Close
File Write / Read
File Delete
File Append
File Truncate
File upload
PHP str replace
PHP substr_replace
PHP Capitalization
PHP Explode
PHP Implode
   IT Jobs
Software Jobs
Networking Jobs
   Model Question Papers
BE Computer Science
MCA
BCA
Others
 
   

PHP - Write/Read Function

File Write

Lets get on to the most useful part of file manipulation, writing. There is really only one main function that is used to write and it’s logically called fwrite.

File Open : Write

Before we can write information to our test file we have to use the function fopen to open the file for writing.

PHP Code:

$fname = “first.txt”;
$f = fopen($fname, ‘w’);
PHP - File Write: fwrite Function

We can use php to write to a text file. The fwrite function allows data to be written to any type of file. Fwrite’s first parameter is the file handle and its second parameter is the string of data that is to be written. Just give the function those two bits of information and you’re good to go!
Below we are writing a couple of names into our test file first.txt and separating them with a carriage return.

PHP Code:

$fname = “first.txt”;
$f = fopen($fname, ‘w’) or die(”can’t open file”);

$data = “How are you?”;
fwrite($f, $data);

$data = “I am fine”;
fwrite($f, $data);
fclose($f);

The $f variable contains the file handle for first.txt. The file handle knows the current file pointer, which for writing, starts out at the beginning of the file.

We wrote to the file first.txt twice. Each time we wrote to the file we sent the string $data that first contained How are you and second contained I am fine. After we finished writing we closed the file using the fclose function.

If you were to open the first.txt file in NOTEPAD or wordpad it would look like this:

How are you? I am fine.

File Write: Overwriting

Now that first.txt contains some data ,when you open an existing file for writing, all the data contained in the file is erased and you start with an empty file. In this example we open our existing file first.txt and write some new data into it.

PHP Code:

$fname = “first.txt”;
$f = fopen($fname, ‘w’) or die(”can’t open file”);

$data = “Welcome”;
fwrite($f, $data);

$data = “TO the world of PHP”;
fwrite($f, $data);
fclose($f)

If you now open the first.txt file you will see that previous data si vanished, and only the data we just wrote is there.

Contents of the first.txt File:

Welcome to the world of PHP

File Read

How to get information from files? In this lesson we will teach you how to read data from a file using various PHP functions.

File Open:Read

Before we can read information from a file we have to use the function fopen to open the file for reading. Here’s the code to read-open the file we created in the PHP File Write lessons.

PHP Code:

$fname = “first.txt”;
$f = fopen($fname, ‘r’);

The file we created in the last lesson was named “first.txt”.

first.txt Contents:

Welcome to the world of PHP

Now that the file is open, with read permissions enabled, we can get started!

FREAD Function

The fread function is used for getting data out of a file. The function requires a file handle, which we have, and an integer to tell the function how much data, in bytes, it is supposed to read.

One character is equal to one byte. If you wanted to read the first five characters then you would use five as the integer.

Code

$fname = “first.txt”;
$f = fopen($fname, ‘r’);
$data = fread($f, 7);
fclose($f);

echo $data;

Display:

Welcome

The first seven characters from the first.txt file are now stored inside $data. You could echo this string, $data.

If you wanted to read all the data from the file, then you need to get the size of the file. The filesize function returns the length of a file, in bytes, which is just what we need! The filesize function requires the name of the file that is to be sized up.

$fname = “first.txt”;
$f = fopen($fname, ‘r’);
$data = fread($f, filesize($fname));
fclose($f);
echo $data;

Display:

Welcome to the world of PHP

PHP - File Read: gets Function

PHP also lets you read a line of data at a time from a file with the gets function.

Suppose content of your new.txt file is.

Hello
Welcome to the world of PHP
We are learning file functions.

Code

$fname = “new.txt”;
$f = fopen($fname, ‘r’);
$data = fgets($f);
fclose($f);
echo $data;

The fgets function searches for the first occurrence of “\n” the newline character.

Display

Hello
Welcome to the world of PHP
We are learning file functions.

<< Previous | Next >>
Home  |  About us  | Privacy  |  Disclaimer  |  Contact us |  Advertise with us | Our Link Partners
All Rights Reserved 2009, CodeTeller.com