Open In App

PHP | fstat( ) Function

The fstat() function in PHP is an inbuilt function which is used to return information about an open file. The file name is sent as a parameter to the fstat() function and it returns an array with the following elements :

Numeric Associative Description
0 dev Device number
1 ino inode number*
2 mode inode protection mode
3 nlink number of links
4 uid userid of owner*
5 gid groupid of owner
6 rdev device type, if inode device
7 size size in bytes
8 atime time of last access (Unix timestamp)
9 mtime time of last modification (Unix timestamp)
10 ctime time of last inode change (Unix timestamp)
11 blksize blocksize of filesystem IO **
12 blocks number of 512-byte blocks allocated **

The fstat() function gathers the statistics of the file opened by the file pointer handle. The fstat() function is similar to the stat() function except that it operates on an open file pointer instead of a filename.



Syntax:

array fstat ( $file )

Parameters: The fstat() function in PHP accepts only one parameters.



Return Value: It returns an array on success with the elements mentioned above.

Exceptions:

Below programs illustrate the fstat() function:

Program 1:




<?php
// Opening a file
$myfile = fopen("gfg.txt", "r");
  
// printing the stats of the opened file
print_r(fstat($myfile));
  
// closing the file
fclose($myfile);
?>

Output:

Array
(
[0] => 0
[1] => 0
[2] => 33206
[3] => 1
[4] => 0
[5] => 0
[6] => 0
[7] => 92
[8] => 1141633430
[9] => 1141298003
[10] => 1138609592
[11] => -1
[12] => -1
[dev] => 0
[ino] => 0
[mode] => 33206
[nlink] => 1
[uid] => 0
[gid] => 0
[rdev] => 0
[size] => 92
[atime] => 1141633430
[mtime] => 1141298003
[ctime] => 1138609592
[blksize] => -1
[blocks] => -1
)

Program 2:




<?php
// Opening a file
$myfile = fopen("gfg.txt", "r");
  
// printing the associative part of the output array
$mystat = fstat($myfile);
print_r(array_slice($mystat, 13));
  
// closing the file
fclose($myfile);
?>

Output:

Array
(
    [dev] => 771
    [ino] => 488704
    [mode] => 33188
    [nlink] => 1
    [uid] => 0
    [gid] => 0
    [rdev] => 0
    [size] => 1114
    [atime] => 1061067181
    [mtime] => 1056136526
    [ctime] => 1056136526
    [blksize] => 4096
    [blocks] => 8
)

Reference : http://php.net/manual/en/function.fstat.php


Article Tags :