2.5 Filesystem Functions
2.5 Filesystem Functions
• Files are usually used to store information such as; configuration settings of a program, simple
data such as contact names against the phone numbers and images, pictures, photos, etc.
• PHP file functions support a wide range of file formats that include; File.txt, File.log, File.csv,
File.gif, file.jpg etc.
• The Filesystem function is used to access and manipulate filesystem. For accessing the files on
the system, the file path will be used.
• Syntax:
file_type function()
PHP File System Functions:
OPENING AND CLOSING FILES
• fopen():
The PHP fopen() function is used to open a file. It requires two arguments stating first the file name
and then mode in which to operate. If an attempt to open a file fails then fopen returns a value of
false otherwise it returns a file pointer which is used for further reading or writing to that file.
Syntax:
fopen($file_name,$mode);
Opens the file for reading and writing only. Places the file pointer at the beginning of the file and
truncates the file to zero length. If files does not exist then it attempts to create a file.
Places the file pointer at the end of the file. If files does not exist then it attempts to create a file.
a+
Opens the file for reading and writing only. Places the file pointer at the end of the file.
<?php
$fileName = "/doc/myFile.txt";
$fp = fopen($fileName,"r");
exit();
}
PHP File System Functions:
• fclose():
After making a changes to the opened file it is important to close it with the fclose() function.
The fclose() function requires a file pointer as its argument and then returns true when the
closure succeeds or false if it fails.
Syntax: fclose($handle);
Here,
• fread():
Once a file is opened using fopen() function it can be read with a function called fread().
This function requires two arguments. These must be the file pointer and the length of the file
expressed in bytes.
The function will stop at the end of the file or when it reaches the specified length, whichever
comes first.
Syntax
fread(file, length);
PHP File System Functions:
Example;
<?php
$file = fopen("test.txt","r");
fread($file,filesize("test.txt"));
fclose($file);
?>
The files length can be found using the filesize() function which takes the file name as its
argument and returns the size of the file expressed in bytes.
PHP File System Functions:
• file_get_contents()
This function will read the entire contents of a file into a variable without the need to open or
close the file.
Example:
<?php
$f = "c:\windows\win.ini";
$f1 = file_get_contents($f);
echo $f1;
?>
PHP File System Functions:
WRITING A FILE:
• fwrite():
The fwrite() function in PHP is an inbuilt function which is used to write to an open file.
The fwrite() function stops at the end of the file or when it reaches the specified length passed as
a parameter, whichever comes first.
Here,
“fwrite” is the PHP function for writing to files
“$filepointer” is the file pointer resource
“$string” is the data to be written in the file.
“$length” is optional, can be used to specify the maximum file length.
PHP File System Functions:
Example:
<?php
$file = fopen("test.txt","w");
fclose($file);
?>
21
PHP File System Functions:
• file_put_contents():
First, it deletes the existing contents of a file then it writes new contents.
Filename - Specifies the path to the file to write to. If the file does not exist, this function will
create one
Data - The data to write to the file. Can be a string, array, or a data stream
context - Optional. Specifies the context of the file handle. Context is a set of options that can
modify the behavior of a stream.
<?php
?>
Output:
21
PHP File System Functions:
• fputs( ):
The fputs() function in PHP is an inbuilt function which is used to write to an open file. The
fputs() function stops at the end of the file or when it reaches the specified length passed as a
parameter, whichever comes first.
bytes to be written.
PHP File System Functions:
Example:
<?php
$file = fopen("test.txt","w");
fclose($file);
?>
output: 21
PHP File System Functions:
COPYING A FILE
Here,
Example: <?php
echo copy("source.txt","target.txt");
PHP File System Functions:
DELETING A FILE:
Example:
<?php
$file = fopen("test.txt","w");
fclose($file);
unlink("test.txt");
?>
PHP File System Functions:
Few more file function:
current buffer.