FileSystem Function in PHP: PART 5

Introduction

In this article I describe the PHP FileSystem functions file, fgetss, file_exists and file_get_contents. 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

PHP File() Function

The PHP filesystem file function reads an entire file into an array and returns the array with each element of the array corresponding to a line of the file, with the newline still attached or returns false on failure.

Syntax

file(path,include_path,context)

Parameters of the file function

The parameters of the function are:

Parameter Description
path It specifies the file to read.
include_path Set this parameter to one (1), if you want to search for the file in the include_path in php.ini file as well.
context It specifies a context for the file.

Example

<?php
echo
"<pre>";
print_r
(file("test.txt"));
?>


Output

file-function-in-php.gif

PHP fgetss() Function

The PHP filesystem function fgetss gets a line from the file pointer and strips the HTML and PHP tags.

Syntax

fgetss(file,length,tags)

Parameters of the fgetss function

The parameters of the function are:

Parameter Description
file It specifies the file to check.
length It specifies the number of bytes to read.
tags It specifies what tags are not to be removed.

Example

<?php
$
file = fopen("test.txt", "r");
while (!feof($file))
{
       echo fgetss($file);
       echo "<
br/>";
}
fclose($file);
?>
 
Output

fgetss-function-in-php.gif

PHP file_exists() Function

The PHP filesystem file_exists function checks whether a file or directory exists and it returns true if the file or directory specified by filename exists otherwise it returns false.

Syntax

file_exists(path)

Parameter in file_exists function

The parameter of the function is:

Parameter Description
path It specifies the path to check.

Example

<?
php
echo
file_exists("test.txt");
?>

Output

file-exists-function-in-php.gif

PHP file_get_contents() Function

The PHP filesystem file_get_contents function reads an entire file into a string and it returns the data read or false on failure.

Syntax

file_get_contents(path,include_path,context,start,max_length)

Parameters in file_get_contents function

The parameters of the function are:

Parameter Description
file It specifies the file to read.
include_path Set this parameter to one (1), if you want to search for the file in the include_path in php.ini file as well.
context It specifies the context file to handle.
start It specifies an offset of where to start reading the file.
max_length It specifies how many bytes to read.

Example

<?
php
echo
file_get_contents("test.txt");
?>


Output

file-get-contents-function-in-php.gif

Up Next
    Ebook Download
    View all
    Learn
    View all