File handling Function
fopen()
The fopen() function is used to open files in PHP.
The first parameter of this function contains the
name of the file to be opened and the second
parameter specifies in which mode the file should
be opened.
If fopen is unable to open the file, it returns 0
(zero) otherwise it will return 1.
2
Syntax and Example
var_name=fopen(file_name,file_open_mode)
<?php
echo $file=fopen(test.txt","r") or die("can't open
file");
?>
The file may be opened in one of the following modes
Modes
Description
Read only. Starts at the beginning of the file
r+
Read/Write. Starts at the beginning of the file
Write only. Opens and clears the contents of
file; or creates a new file if it doesn't exist
Read/Write. Opens and clears the contents of
file; or creates a new file if it doesn't exist
Append. Opens and writes to the end of the
file or creates a new file if it doesn't exist
Read/Append. Preserves file content by
writing to the end of the file
Write only. Creates a new file. Returns
FALSE and an error if file already exists
Read/Write. Creates a new file. Returns
FALSE and an error if file already exists
w+
a
a+
x
x+
fwrite()
The fwrite function allows data to be written to
any type of file and an open file.
The function will stop at the end of the file or
when it reaches the specified length, whichever
comes first.
This function returns the number of bytes written
on success, or FALSE on failure.
5
Syntax and Example
var_name =fwrite(file,string,length)
<?php
$my_file = 'file.txt';
$f_open = fopen($my_file, 'w') or die("can't open
file");
$data=fwrite($f_open,"Computer Science
College");
echo $data; ?>
6
fread()
The fread() reads from an open file.
The function will stop at the end of the file or
when it reaches the specified length, whichever
comes first.
fread() function returns the read string on success,
or FALSE on failure.
Syntax and Example
Syntax:
$file_contents =fread(file,length)
<?php
$my_file = 'file.txt';
$f_open = fopen($my_file, 'r') or die("can't open
file");
$data = fread($f_open,20);
echo $data; ?>
8
$myFile = "testFile.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, 5);
fclose($fh);
echo $theData;
$myFile = "testFile.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
echo $theData;
10
fclose()
The fclose() function closes an open file.
fcose() function returns TRUE on success or
FALSE on failure.
11
Syntax and Example
Syntax:
fclose(file)
<?php
$f_open = fopen('file.txt', 'r') or die("can't open
file");
fclose($f_open);
?>
12
file_exists()
file_exists()
function is used to check whether a
file or directory exists or not.
Return
TRUE if the file or directory specified
by file_name exists otherwise FALSE.
13
Syntax and Example
Syntax:
var_name=file_exists (file_name)
<?php
if(file_exists('file.txt'))
{ echo "File is Found"; }
else
{
?>
echo "File isnot Found";
}
14
is_readable
The is_readable() function is used to check whether
the specified file is readable or not.
TRUE if file_name exists and is readable, FALSE
otherwise.
15
Syntax and Example
is_readable(file_name)
<?php
if(is_readable('file.txt'))
{
echo "File is Readable";
}
else
{ echo "File isnot Readable";
} ?>
16
is_writable()
The is_writable() function is used to check whether
the specified file is writeable.
TRUE if file_name exists and is writable.
17
Syntax and Example
is_writable(file_name)
<?php
if(is_writable('file.txt'))
else
?>
echo "File is Writable"; }
echo "File isnot Writable"; }
18
fgets()
The fgets() function is used to get line from file
pointer of an open file.
The fgets() function stops returning on a new line, at
the specified length, or at EOF, whichever comes
first.
This function returns FALSE on failure.
19
Syntax and Example
fgets(file_handler, byte_length)
Name
Description
file_handler
When a file is successfully opened by fopen() or
fsockopen() it returns a resource ID, which is
referred as file handler or file pointer. The fgets()
function uses this ID to return a series of bytes
from an open file
byte_length
Specifies the number of bytes to read. Default
length is 1024.
20
<?php
//$file = fopen("file.txt","r");
echo fgets($file,10);
echo fgets($file);
fclose($file);
?>
21
fgetc()
The fgetc() function reads a single character from a
open file pointed by file handler..
A string containing a single character and FALSE on
EOF.
22
Syntax and Example
fgetc(file_handler)
<?php
$file = fopen("file.txt","r");
echo fgetc($file);
fclose($file);
?>
23
file()
The file() reads whole file into an array.
Each array element contains a line from the file, with
newline still attached.
24
Syntax and Example
file(path,include_path,context)
<?php
print_r(file("file.txt"));
?>
25
file_get_contents()
The file_get_contents() reads a whole file into a
string.
file_get_contents
(file_name, include_path_name, context,
star_position, max_length)
26
Parameter
Description
path
Required. Specifies the file to read
include_path
Optional. Set this parameter to '1' if you want to search for
the file in the include_path (in php.ini) as well
context
Optional. Specifies the context of the file handle. Context is a
set of options that can modify the behavior of a stream. Can
be skipped by using NULL.
start
Optional. Specifies where in the file to start reading. This
parameter was added in PHP 5.1
max_length
Optional. Specifies how many bytes to read. This parameter
was added in PHP 5.1
27
Example
<?php
echo file_get_contents("file.txt");
?>
28
file_put_contents()
The file_put_contents() function writes a string
to a file.
file_put_contents(file,data,mode,context)
29
Syntax and Example
Parameter
Description
file
Required. Specifies the file to write to. If the file does not
exist, this function will create one
Required. The data to write to the file. Can be a string, an array or a
data stream
data
mode
Optional. Specifies how to open/write to the file. Possible values:
FILE_USE_INCLUDE_PATH
FILE_APPEND
LOCK_EX
context
Optional. Specifies the context of the file handle. Context is a set of
options that can modify the behavior of a stream.
30
<?php
$str="Atmiya College Rajkot Comp. Dept.";
file_put_contents("file.txt",$str);
echo file_get_contents("file.txt");
?>
31
ftell()
The ftell() function is used to fetch the current
position of the file pointer of an open file.
ftell(file_handler)
32
Questions?