FileSystem Function in PHP: PART 6

Introduction

In this article I describe the PHP FileSystem functions file_put_contents, fileatime, filectime and filegroup. To learn some other FileSystem functions, go to:

  1. FileSystem Function in PHP: PART 1  
  2. FileSystem Function in PHP: PART 2  
  3. FileSystem Function in PHP: PART 3  
  4. FileSystem Function in PHP: PART 4  
  5. FileSystem Function in PHP: PART 5

PHP file_put_contents() Function

The PHP file_put_contents function is used to write strings into a file and it returns the number of bytes that were written to the file or false on failure.

Syntax

file_put_contents(file,data,mode,context)

Parameters in file_put_contents function

The parameters of the function are:

Parameter Description
file It specifies the path of the file where to write the data.
data It specifies the data to write to the file.
mode It specifies how to open or write to the file and its possible values are:
  • FILE_USE_INCLUDE_PATH
  • FILE_APPEND
  • LOCK_EX
context It specifies the context of the file handle.

Example
 
<?php
echo
file_put_contents("test.txt","Welcome in MCN Solution!");
?>

 
Output


file-put-content-function-in-php.gif

PHP fileatime() Function

The PHP fileatime function gives the last access of time of the specified file, in other words it returns the time the file was last accessed or false on failure. The time is returned as a Unix timestamp.

Syntax

fileatime(filename)

Parameter in fileatime function

The parameter of the function is:

Parameter Description
filename It specifies the file to check.

Example

<?php

$time = time();

echo "current date and times is ".date("m/d/y H:i:s ", $time);

echo "<br>current File Access Time is ".date("m/d/y H:i:s ", fileatime("test.txt"));

?>
 
Output

fileatime-function-in-php.gif 

PHP filectime() Function

The PHP filectime function is used to get the last inode change time for a file, in other words it returns the time the file was last accessed or false on failure. The time is returned as a Unix timestamp..

Syntax

filectime(filename)

Parameter in filectime function

The parameter of the function is:

Parameter Description
filename It specifies the file to check.

Example

<?php

echo filectime("test.txt")."</br>";

echo "Last change: ".date("F d Y H:i:s.",filectime("test.txt"));

?>

Output

filectime-function-in-php.gif 

PHP filegroup() Function

The PHP filegroup function is used to give the file group and it returns the group id of the file or false on failure. The group id is returned in the numeric format.

Syntax

filegropup(filename)

Parameter in filegroup function

The parameter of the function is:

Parameter Description
filename It specifies the file to check.

Example

<?php

echo filegroup("test.txt");

?>


Output

filegroup-function-in-php.gif

Up Next
    Ebook Download
    View all
    Learn
    View all