FileSystem Function in PHP: PART 4

Introduction

In this article I describe the PHP FileSystem functions fflush, fgetc, fgetcsv and fgets. 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
PHP fflush() function

The PHP filesystem fflush function flushes the output to a file and it returns true on success and false on failure.

Syntax


fflush(file)

Parameter in fflush function

The parameter of the function is:


Parameter Description
file It specifies the open file to check.

Example

An example of the
function is:

<?php
$
filename = 'test.txt';
$
file = fopen($filename, 'r+');
rewind
($file);
fwrite
($file, 'great day');
fflush
($file);
fclose
($file);
?>
 
PHP fgetc() function

The PHP filesystem fgetc function
gets a character from the file pointer and this function returns a string containing a single character read from the file pointed to by the handle or returns false on EOF.

Syntax
 
fgetc(file)

Parameter in fgetc function

The parameter of the function is:


Parameter Description
file It specifies the open file to check.

Example

An example of the
function is:

<?php
$
file = fopen("test.txt", "r");
while (!feof($file))
{
       echo fgetc($file);
}
fclose($file);

?>
 
Output

fgetc-function-in-php.jpg 

PHP fgetcsv() function

The PHP filesystem fgetcsv function gets a line from the file pointer and parses for CSV fields and returns an indexed array containing the fields read.


Syntax


fgetcsv(file, length,separator,enclousure)

Parameter in fgetcsv function

The parameters of the function are:


Parameter Description
file It specifies the file to check.
length It specifies the maximum length of a line.
separator It specifies  a character that specifies the field separator.
file It specifies a character that specifies the field enclosure character..

Example

An example of the
function is:

<?
php
$
file = fopen("test.csv","r");
print_r(fgetcsv($file));
fclose($file);

?>


Output


getcsv-function-in-php.jpg

PHP fgets() function

The PHP filesystem fgets a line from the file pointer and this function returns false on failure.

Syntax

fgets(file, length,separator,enclousure)

Parameters in fgets function

The parameters of the function are:


Parameter Description
file It specifies the file to read from.
file It specifies the number of bytes to read.

Example

An example of the
function is:

<?
php
$
file = fopen("test.csv","r");
print_r(fgetcsv($file));
fclose($file);

?>


Output

fgets-function-in-php.jpg


Similar Articles