PHP File Handling Function
PHP File Handling Function
fopen()
var_name=fopen(file_name,file_open_mode)
<?php
echo $file=fopen(test.txt","r") or die("can't open
file");
?>
Description
r+
w+
a
a+
x
x+
fwrite()
var_name =fwrite(file,string,length)
<?php
$my_file = 'file.txt';
fread()
$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";
echo $theData;
10
fclose()
The fclose() function closes an open file.
FALSE on failure.
11
fclose(file)
<?php
file_exists()
file_exists()
Return
13
var_name=file_exists (file_name)
<?php
if(file_exists('file.txt'))
{ echo "File is Found"; }
else
{
?>
}
14
is_readable
15
is_readable(file_name)
<?php
if(is_readable('file.txt'))
{
echo "File is Readable";
}
else
{ echo "File isnot Readable";
} ?>
16
is_writable()
17
is_writable(file_name)
<?php
if(is_writable('file.txt'))
else
?>
18
fgets()
19
Name
Description
file_handler
byte_length
20
<?php
//$file = fopen("file.txt","r");
echo fgets($file,10);
echo fgets($file);
fclose($file);
?>
21
fgetc()
22
fgetc(file_handler)
<?php
$file = fopen("file.txt","r");
echo fgetc($file);
fclose($file);
?>
23
file()
24
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
26
Parameter
Description
path
include_path
context
start
max_length
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
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
context
30
<?php
file_put_contents("file.txt",$str);
echo file_get_contents("file.txt");
?>
31
ftell()
ftell(file_handler)
32
Questions?