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.

  • $file: It is a mandatory parameter which specifies the file.

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

Exceptions:

  • The results from this function will differ from server to server. The array may contain the number index, the name index, or both.
  • The fstat() function is similar to stat() function, except that with this function the file must be open.
  • atime element will not be updated by simple read access to the file.

Below programs illustrate the fstat() function:

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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);
?>

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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);
?>

chevron_right


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



My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.