FileSystem Function in PHP: PART 10

Introduction

In this article I describe the PHP FileSystem functions fread, fscanf, fseek and stat. 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
  6. FileSystem Function in PHP: PART 6
  7. FileSystem Function in PHP: PART 7
  8. FileSystem Function in PHP: PART 8
  9. FileSystem Function in PHP: PART 9

PHP fread() Function

The PHP FileSystem fread function reads data from an external file and returns the string read on success or false on failure.

Syntax

fread(file,length)


Parameters in
fread function

The parameters of the function are:
 

Parameter Description
file It specifies the open file to read from.
length It specifies the maximum number to read from

Example of fread function

An example of the function is:


<?
php
$data = fopen("test.txt","r
");

fread($data,"
10");
fclose($data);
?>


PHP fscanf() Function

The PHP FileSystem fscanf function parses input from a file according to a specified format and it will return an array, if only two parameters were passed to this function.

Syntax

fscanf(file,format,mixed)


Parameters in
fscanf function

The parameter of the function are:
 

Parameter Description
file It specifies the file to check.
format It specifies the format and the specific values are:
  • %% - It returns a percent sign.
  • %b - It returns the binary number.
  • %c - The character according to the ASCII value.
  • %d - Signed decimal number.
  • %e - Scientific notation (e.g. 1.2e+2).
  • %u - Unsigned decimal number.
  • %f - Floating-point number.
  • %F - Floating-point number.
  • %o - Octal number
  • %s - It specifies string.
  • %x - It returns Hexadecimal number in lowercase letters.
  • %X - It returns Hexadecimal number in uppercase letters.
mixed It is an optional assigned value parameter.

Example of fscanf function

An example of the function is:


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

while($array=fscanf($file,"
%s\t%s"))
{
for($x=0;$x<sizeof($array);$x++)
{
echo $array[$x];
if ($x%2!=0)
echo "<
br>";
}
}
?>


Output

fscanf-function-in-php.jpg

PHP fseek() Function

The PHP FileSystem fseek function seeks on a file pointer. This function basically moves the file pointer from its current position to a new position and this function returns 0 (zero) on success or -1 on failure.

Syntax

fseek(file,offset,whence)

Parameters in fseek function

The parameters of the function are:

Parameter Description
file It specifies the open file to seek in.
offset It specifies the new position.
whence It is an optional parameter and possible values are:
SEEK SET - Set position equal to offset bytes.
SEEK_CUR - Set position to current location plus offset.
SEEK_END - Set position to end of file.

Example of fseek function

An example of the function is:


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

fgets($file);
fseek($file,240);
?>

 
PHP fstat() Function

The PHP FileSystem fstat function gets the information about a file using an open file pointer and it returns an array with the statistics of the file or you can simply say it returns the information about an open file.

Syntax

fstat(file)

Parameters in fstat function

The parameter of the function is:

Parameter Description
file It specifies the open file to check.

Example of fstat function

An example of the function is:


<?
php
$file_handler = fopen('test.txt','r
');

echo "<
pre>";
print_r(fstat($file_handler));
fclose($file_handler);
?>

 
Output

fstat-function-in-php.jpg