file_put_contents()

This function writes a string to a file.

Syntax: file_put_contents(file,data,mode,context)

Description:

The Parameter “file” is Required. It Specifies the file to write to. If the file does not exist, this function will create one.

The second Parameter “data” is Required. The data to write to the file. Can be a string, an array or a data stream.

The third Parameter “mode” is Optional.It Specifies how to open/write to the file. Possible values:

FILE_USE_INCLUDE_PATH
FILE_APPEND
LOCK_EX

The third Parameter “context” is Optional. It Specifies the context of the file handle. Context is a set of options that can modify the behavior of a stream.

This function returns the number of character written into the file on success, or FALSE on failure.

Example:
Code:

echo file_put_contents("test.txt","Php code is executed on the server.");

Output:

35

fileatime()

This function returns the last access time of the specified file.

Syntax: fileatime(filename)

Description:

The Parameter filename is Required. It Specifies the file to check.

This function returns the last access time as a Unix timestamp on success, FALSE on failure.

Example:
Code:

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

Output:

1307162313
Last visit: June 04 2011 04:38:33.

file_get_contents()

The function reads a file into a string.

Syntax: file_get_contents(path,include_path,context,start,max_length)
Description:

The first parameter “path” is Required. It specifies the file to read.

The second parameter include_path is Optional. It Set this parameter to ‘1’ if you want to search for the file in the include_path (in php.ini) as well.

The third parameter “context” is Optional. It specifies the context of the file handle. Context is a set of options that can modify the behavior of a stream. Can be skipped by using NULL.

The fourth parameter “start” is Optional. It Specifies where in the file to start reading. This parameter was added in PHP 5.1.

The fifth parameter “max_length” is Optional. It specifies how many bytes to read. This parameter was added in PHP 5.1.

Example:
Code:

echo file_get_contents("test.txt");

Output:

PHP is an interpreted language.

is_readable()

The is_readable() function checks whether the specified file is readable.

Syntax: is_readable(file)

Description:

The parameter “file” is Required. It specifies the file to check.

This function returns TRUE if the file is readable.

Example:
Code:

$file = "test.txt";
if(is_readable($file))
 echo ("$file is readable");
  }
else
  {
  echo ("$file is not readable");
 }

Output:

Filesize()

This function returns the size of the specified file.

Syntax: filesize(filename)

Description:

The argument “filename” is Required. It specifies the file to check.

This function returns the file size in bytes on success or FALSE on failure.

Example:
Code:

echo filesize("test.txt");

Output:

34

Page 1 of 3123
eXTReMe Tracker