File Handling in PHP

Functions Used in File Handling

1) fopen(filename,mode) – used for opening a file with specific mode.
2) fclose(fp) – used to close the file. (fp -> filepointer)
3) feof(fp) – used to find the End of File
4) fgetc(fp) – Gets character from file pointer
5) fgets(fp) – Gets a line from file pointer
6) fread(fp,size) – Binary-safe file read
7) fscanf(fp,format) – Parses input from a file according to a format
8) fwrite(fp,string) – Binary-safe file write
9) file_put_contents(fp,string) – Write a string to a file
10) file_get_contents(fp) – Reads entire file into a string
11) file(fp) – Reads entire file into an array
12) file_exists(fp) – Checks whether a file or directory exists
13) is_readable(fp) – Tells whether the filename is readable
14) is_writable(fp) – Tells whether the filename is writable
15) is_file(path) – Tells whether the filename is a regular file
16) is_dir(path) – Tells whether the filename is a directory
17) is_link(path) – Tells whether the filename is a symbolic link
18) readlink(path) – Returns the target of a symbolic link
19) readdir – Read entry from directory handle
20) glob – Find pathnames matching a pattern
21) filesize(fp) – Gets file size
22) filetype(fp) – Gets file type
23) fprintf(fp,format) – Write a formatted string to a stream
24) fstat(fp) – Gets information about a file using an open file pointer

File Opening Modes

Modes Description
r Read only. Starts at the beginning of the file
r+ Read/Write. Starts at the beginning of the file
w Write only. Opens and clears the contents of file; or creates a new file if it doesn’t exist
w+ Read/Write. Opens and clears the contents of file; or creates a new file if it doesn’t exist
a Append. Opens and writes to the end of the file or creates a new file if it doesn’t exist
a+ Read/Append. Preserves file content by writing to the end of the file
x Write only. Creates a new file. Returns FALSE and an error if file already exists
x+ Read/Write. Creates a new file. Returns FALSE and an error if file already exists

File Reading and Writing

Method 1 : File Reading Using Bytes

<?php
/*
* Author : Antony
* Created on 22-Feb-08
* Program Name : filereadbytes.php
*/
$filename = “sample.txt”;
$fp = fopen($filename,”r”) or exit(“Unable to open file…”);
while(!feof($fp))
{
$content = fread($fp,filesize($fp));
echo $content;
}
fclose($fp);
?>

Method 1 : File Reading Using Character

<?php
/*
* Author : Antony
* Created on 22-Feb-08
* Program Name : fileread.php
*/
$filename = “/home/anthoniraj/textfiles/sample.txt”;
$fp = fopen($filename,”r”) or exit(“Unable to open file…”);
while(!feof($fp))
{
echo fgetc($fp);
echo “<br />”;
}
fclose($fp);
?>

Method 1 : File Reading Using Lines

<?php
/*
* Author : Antony
* Created on 22-Feb-08
* Program Name : filereadline.php
*/
$filename = “sample.txt”;
$fp = fopen($filename,”r”) or exit(“Unable to open file…”);
while(!feof($fp))
{
echo fgets($fp);
echo “<br />”;
}
fclose($fp);
?>

Method 4: Writing Contents to File

<?php
/*
* Author : Antony
* Created on 22-Feb-08
* Program Name : filewritechar.php
*/
$filename = “sample.txt”;
$fp = fopen($filename,”w”) or exit(“Unable to open file…”);
$content = “Sample File Contents”;
if(fwrite($fp,$content)==true)
{
echo “Successfully written”;
}
else
{
echo “Failed to write”;
}

?>

Simple Counter Program

<?php
/*
* Author : Antony
* Created on 22-Feb-08
* Program Name : counter.php
*/
$path = “counter.txt”;
$fp = fopen($path, “r”);
$count = fread($fp, 1024);
fclose($fp);

$count = $count + 1;
echo “<p>Page views: ” . $count . “</p>”;

$fp = fopen($path, “w”);
fwrite($fp, $count);
fclose($fp);

?>

Leave a Response