FileSystem Function in PHP: PART 2

Introduction

In this article I describe the PHP FileSystem functions clearstatcache, copy, dirname and disk_free_space. To learn some other filesystem functions, go to:

  1. FileSystem Function in PHP: PART 1

PHP clearstatcache() function

The PHP filesystem clearstatcache
function is used to clear the cache associated with a file and this function returns no value. If you are performing multiple operations on the file use the following functions:

  • stat()
  • lstat()
  • file_exists()
  • is_writable()
  • is_readable()
  • is_executable()
  • is_link()
  • filectime()
  • filegroup()
  • fileowner()
  • ileperms() and so on..

Then the PHP will store the cache in memory. You need to clear the cache if you are using these functions in the same PHP program multiple times and this is done with the clearstatcache() function.

Syntax

clearstatcache()

Example

An example of the
function is:

<?
php
$file=fopen("test.txt","w
");

echo "<
br>file-size before clearstatcache() function ..<br>";
echo filesize("test.txt");
clearstatcache();
$p="welcome to c-sharapcorner.com and This article describes how to clear the cache associated with a file.";
fwrite($file,$p);
echo "<br>file-size after clearstatcache() function ..<br>";
echo filesize("test.txt");
?>

Output

clearstatcache-function-in-php.gif

PHP copy() function

The PHP filesystem copy
function will make a copy of a file that you specify on the server and the function returns true on success and false on failure.

Syntax

copy(source_file, destination_file)

Parameter in copy function

The parameters of the function are:

Parameter Description
source_file It specifies the path to the source file.
destination_file It specifies the destination path.

Example

An example of the
function is:

<?
php
echo
copy("test.txt","retest.txt");
?>

Output

copy-function-in-php.gif

PHP dirname() function

The PHP filesystem dirname
function is used to get the full directory path name of the file.

Syntax

dirname(path)

Parameter in dirname function

The parameter of the function is:

Parameter Description
path It specifies the path to check.

Example

An example of the
function is:

<?
php
$dirpath=dirname("C:\wamp\www\zend_framework\bin\zendy\tests\bootstrap.php
");

echo $dirpath;
echo"<
br>";
$dirpath1=dirname("C:\wamp\www\basename.php");
echo $dirpath1;
?>


Output

dirname-function-in-php.gif

PHP disk_free_space() function

The PHP filesystem disk_free_space function returns the available space of the filesystem or disk partition (in bytes).


Syntax

disk_free_space(directory)

Parameter in disk_free_space function

The parameter of the function is:

Parameter Description
directory It specifies the directory to check.

Example

An example of the
function is:

<?
php
echo
"Total Free space on drive C (in Bytes) is: ";
echo
disk_free_space("C:")
?>

Output

disk-free-space-function-in-php.gif


Similar Articles