PHP | stat( ) function Last Updated : 16 May, 2018 Comments Improve Suggest changes Like Article Like Report The stat() function in PHP is an inbuilt function which is used to return information of a file. The stat(0) function returns statistics of a file which is an array with the following elements : [0] or [dev] - Device number [1] or [ino] - Inode number [2] or [mode] - Inode protection mode [3] or [nlink] - Number of links [4] or [uid] - User ID of owner [5] or [gid] - Group ID of owner [6] or [rdev] - Inode device type [7] or [size] - Size in bytes [8] or [atime] - Last access (as Unix timestamp) [9] or [mtime] - Last modified (as Unix timestamp) [10] or [ctime] - Last inode change (as Unix timestamp) [11] or [blksize] - Blocksize of filesystem IO [12] or [blocks] - Number of blocks allocated The stat() function accepts the filename as a parameter and returns an array with the above-mentioned elements on success and False on failure. If filename is a symbolic link then statistics are from the file itself, not the symlink. Syntax: stat(filename) Parameters Used: The stat() function in PHP accepts one parameter. filename : It specifies the filename of the file whose statistics you want to know. Return Value: It returns array with the above-mentioned elements on success and False on failure. Errors And Exception The results of the stat() function differs from server to server. The result of the stat() function are cached and therefore the clearstatcache() function should be used to clear the cache. The stat() function generates an E_WARNING on failure. On Windows platforms the groupid of owner, userid of owner and inode number are always 0. For files which are larger than 2GB some filesystem functions may return unexpected results since PHP's integer type is signed and many platforms use 32bit integers. Examples: Input : $test = stat('gfg.txt'); echo 'Access time: ' .$test['atime']; echo 'Modification time: ' .$test['mtime']; echo 'Device number: ' .$test['dev']; Output :Access time: 1141666750 Modification time: 1135897503 Device number: 0 Input : $test = stat('gfg.txt'); echo 'Access time: ' .$test[8]; echo 'Modification time: ' .$test[9]; echo 'Device number: ' .$test[0]; Output : Access time: 1141666750 Modification time: 1135897503 Device number: 0 Input : $test = stat('gfg.txt'); $access_time = $stat['atime'] + 18000; if (touch($test, time(), $access_time)) { echo 'Access time changed to 5 hours in the past!'; } else { echo 'Access time could not be changed.'; } Output : Access time changed to 5 hours in the past! Below programs illustrate the stat() function. Suppose there is a file named "gfg.txt" Program 1 php <?php $test = stat('gfg.txt'); //using stat() along with name index to display access time echo 'Access time: ' .$test['atime']; //using stat() along with name index to display modification time echo '<br />Modification time: ' .$test['mtime']; //using stat() along with name index to display device number echo '<br />Device number: ' .$test['dev']; ?> Output: Access time: 1141666750 Modification time: 1135897503 Device number: 0 Program 2 php <?php $test = stat('gfg.txt'); //using stat() along with number index to display access time echo 'Access time: ' .$test[8]; //using stat() along with number index to display modification time echo '<br />Modification time: ' .$test[9]; //using stat() along with number index to display device number echo '<br />Device number: ' .$test[0]; ?> Output: Access time: 1141666750 Modification time: 1135897503 Device number: 0 Program 3 php <?php $test = stat('gfg.txt'); //changing access time to 5 hours in the past $access_time = $stat['atime'] + 18000; //using touch() function to change the access time if (touch($test, time(), $access_time)) { echo 'Access time changed to 5 hours in the past!'; } else { echo 'Access time could not be changed.'; } ?> Output: Access time changed to 5 hours in the past! Reference: http://php.net/manual/en/function.stat.php Comment More infoAdvertise with us S Shubrodeep Banerjee Follow Improve Article Tags : Misc Web Technologies PHP PHP-function Practice Tags : Misc Similar Reads PHP basename( ) Function The basename() function in PHP is an inbuilt function which is used to return the base name of a file if the path of the file is provided as a parameter to the basename() function. Syntax: string basename ( $path , $suffix ) Parameters: The basename() function in PHP accepts two parameters which are 2 min read PHP chgrp( ) Function The chgrp() function in PHP is an inbuilt function that is used to change the user group of the specified file. It returns true on success and false on failure. Only the superuser has the right to change the group of a file arbitrarily. Syntax: bool chgrp ( $filename, $group ) Parameters: The chgrp( 2 min read PHP chmod( ) Function The chmod() function in PHP is an inbuilt function which is used to change the mode of a specified file to a specific mode given by the user. The chmod() function changes the permissions of the specified file and returns true on success and false on failure. Syntax: bool chmod ( string $filename, i 2 min read PHP chown( ) Function The chown() function in PHP is an inbuilt function which is used to change the owner of the specified file. It returns true on success and false on failure. Only the superuser has the right to change the owner of a file. Syntax: bool chown ( $filename, $user ) Parameters: The chown() function in PHP 2 min read PHP copy( ) Function The copy() function in PHP is an inbuilt function which is used to make a copy of a specified file. It makes a copy of the source file to the destination file and if the destination file already exists, it gets overwritten. The copy() function returns true on success and false on failure. Syntax: bo 2 min read PHP dirname( ) Function The dirname() function in PHP is an inbuilt function which is used to return the directory name of a given path. The dirname() function is used to parent directory's path i.e levels up from the current directory. The dirname() function returns the path of a parent directory which includes a dot ('.' 2 min read PHP disk_free_space( ) Function The disk_free_space() function in PHP is an inbuilt function which is used to return the amount of free space in a specified directory. The disk_free_space() function denotes the free space in bytes. It returns the available space on a filesystem or on a disk partition. The disk_free_space() functio 2 min read PHP disk_total_space( ) Function The disk_total_space() function in PHP is an inbuilt function which is used to return the total space of a specified directory. The disk_total_space() function denotes the total space in bytes. It returns the total space on a filesystem or on a disk partition. The disk_total_space() function returns 2 min read PHP fclose() Function The fclose() function in PHP closes a file that was previously opened by fopen(). Closing a file releases the resource associated with it and makes sure all the data written to the file is properly saved. Not closing files can lead to resource leaks or incomplete data writing.Syntax:bool fclose(reso 2 min read PHP feof( ) Function The feof() function in PHP is an inbuilt function which is used to test for the end-of-file on a file pointer. It checks if the "end-of-file" has been reached or not. The feof() function is used for looping through the content of a file if the size of content is not known beforehand. The feof() func 2 min read Like