Skip to content
Related Articles

Related Articles

PHP | fstat( ) Function
  • Last Updated : 14 Jun, 2018

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 :

NumericAssociativeDescription
0devDevice number
1inoinode number*
2modeinode protection mode
3nlinknumber of links
4uiduserid of owner*
5gidgroupid of owner
6rdevdevice type, if inode device
7sizesize in bytes
8atimetime of last access (Unix timestamp)
9mtimetime of last modification (Unix timestamp)
10ctimetime of last inode change (Unix timestamp)
11blksizeblocksize of filesystem IO **
12blocksnumber 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:




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

My Personal Notes arrow_drop_up
Recommended Articles
Page :