Introduction
In this article I describe the PHP FileSystem functions filesize, filetype, flock and fnmatch. To learn some other FileSystem functions, go to:
- FileSystem Function in PHP: PART 1
- FileSystem Function in PHP: PART 2
- FileSystem Function in PHP: PART 3
- FileSystem Function in PHP: PART 4
- FileSystem Function in PHP: PART 5
- FileSystem Function in PHP: PART 6
- FileSystem Function in PHP: PART 7
PHP filesize() Function
The PHP filesize function gets the file size, in other words it returns the file size in bytes on success or false on failure.
Syntax
Parameter in filesize function
The parameter of the function is:
Parameter |
Description |
filename |
It specifies the file to check. |
Example
An example of the function is:
<?php
echo filesize("test.txt");
?>
Output
PHP filetype() Function
The PHP filetype function gets the type of file, basically this function returns one of the seven possible values on success or false on failure.
Values are:
Possible return values:
- fifo
- char
- dir
- block
- link
- file
- unknown
Syntax
Parameter in filetype function
The parameters of the function is:
Parameter |
Description |
filename |
It specifies the file to check. |
Example
An example of the function is:
<?php
echo filetype("test.txt")
?>
Output
PHP flock() Function
The PHP flock function provides a file locking capability that allows you to read from and write to a file and it return true on success and false on failure.
Syntax
Parameter in flock function
The parameters of the function are:
Parameter |
Description |
file |
It specifies an open file to lock or relase. |
lock |
It specifies what kind of lock to use.
values are:
- LOCK_EX - Exclusive lock (writer), prevents other processes from accessing the file.
- LOCK_UN - Release a shared or exclusive lock
- LOCK_SH - Shared lock (reader), allows other processes to access the file
|
block |
The optional thrid argument is set to TRUE if the lock should block. |
Example
An example of the function is:
<?php
$file = fopen("test.txt","w+");
if (flock($file,LOCK_SH))
{
fwrite($file,"Welcome at MCN Solution!");
}
else
{
echo "locking problem in file";
}
?>
PHP fnmatch() Function
The filesystem fnmatch function matches a filename against a pattern and it returns true if there is match else it returns false.
Syntax
fnmatch(pattern,string,flag) |
Parameter in fnmatch function
The parameters of the function are:
Parameter |
Description |
pattern |
It specifies the pattern to search for. |
string |
It specifies the string or file check. |
flag |
It is an optional parameter and the value of flag mya be any combination of the following flags:
- FNM_NOESCAPE - Disable backslash escaping.
- FNM_PATHNAME - Slash in string only matches slash in the given pattern.
- FNM_PERIOD - Leading period in string must be exactly matched by period in the given pattern.
- FNM_CASEFOLD - Caseless match. Part of the GNU extension.
|
Example
An example of the function is:
<?php
$str= "Work is Worship !";
if (fnmatch("*gr[ae]y", $str)) {
echo "It is fnmatch function ...";
}
?>