The Server Pages
:: TSP Menu
- Home
- Authors
- Glossary
- RSS Feed

:: Servers
- CPanel
- Hosting
- Linux
- Windows

:: Webmaster
- Databases
- Design
- Search Engines

:: Scripts
- Scripts

:: Documentation
- PHP Manual

:: Other
- Errors & Solutions
- TechPunt
- Wojjie

search for in the

symlink> <set_file_buffer
[edit] Last updated: Sat, 12 May 2012

view this page in


(PHP 4, PHP 5)

statGives information about a file


array stat ( string $filename )

Gathers the statistics of the file named by filename. If filename is a symbolic link, statistics are from the file itself, not the symlink.

lstat() is identical to stat() except it would instead be based off the symlinks status.



Path to the file.

Return Values

stat() and fstat() result format
Numeric Associative (since PHP 4.0.6) 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 **
* On Windows this will always be 0.

** Only valid on systems supporting the st_blksize type - other systems (e.g. Windows) return -1.

In case of error, stat() returns FALSE.

Note: Because PHP's integer type is signed and many platforms use 32bit integers, some filesystem functions may return unexpected results for files which are larger than 2GB.


Upon failure, an E_WARNING is emitted.


Version Description
4.0.6 In addition to returning these attributes in a numeric array, they can be accessed with associative indices, as noted next to each parameter


Example #1 stat() example

/* Get file stat */
$stat stat('C:\php\php.exe');

 * Print file access time, this is the same 
 * as calling fileatime()
echo 'Access time: ' $stat['atime'];

 * Print file modification time, this is the 
 * same as calling filemtime()
echo 'Modification time: ' $stat['mtime'];

/* Print the device number */
echo 'Device number: ' $stat['dev'];

Example #2 Using stat() information together with touch()

/* Get file stat */
$stat stat('C:\php\php.exe');

/* Did we failed to get stat information? */
if (!$stat) {
'stat() call failed...';
} else {
     * We want the access time to be 1 week 
     * after the current access time.
$atime $stat['atime'] + 604800;

/* Touch the file */
if (!touch('some_file.txt'time(), $atime)) {
'Failed to touch file...';
    } else {
'touch() returned success...';



Note that time resolution may differ from one file system to another.

Note: The results of this function are cached. See clearstatcache() for more details.


As of PHP 5.0.0, this function can also be used with some URL wrappers. Refer to Supported Protocols and Wrappers to determine which wrappers support stat() family of functionality.

See Also

  • lstat() - Gives information about a file or symbolic link
  • fstat() - Gets information about a file using an open file pointer
  • filemtime() - Gets file modification time
  • filegroup() - Gets file group

symlink> <set_file_buffer
[edit] Last updated: Sat, 12 May 2012
add a note add a note User Contributed Notes stat
digitalaudiorock at hotmail dot com 12-Dec-2008 08:23
Regarding the stat() on files larger than 2GB on 32 bit systems not working, note that the behavior appears to differ between Linux and Windows.  Under Windows there's so way to know whether or not this failed.

It's been my experience that under Linux, performing a stat() on files that are too large for the integer size generates a warning and returns false.  However under Windows it silently truncates the high order bits of the size resulting in an incorrect number.  The only way you'd ever know it failed is in the event that the truncation happened to leave the sign bit on resulting in a negative size.  That is, there is _no_ reliable way to know it failed.

This is true of filesize() as well.

webmaster at askapache dot com 26-Nov-2008 01:20
This is a souped up 'stat' function based on
many user-submitted code snippets and

Give it a filename, and it returns an array like stat.


function alt_stat($file) {
$ss) return false; //Couldnt stat file
$t=decoct($ss['mode'] & 0170000); // File Encoding Bit
$str =(array_key_exists(octdec($t),$ts))?$ts[octdec($t)]{0}:'u';
'octal1'=>sprintf("%o", ($ss['mode'] & 000777)),
'octal2'=>sprintf("0%o", 0777 & $p),
'decimal'=>sprintf("%04o", $p),
'realpath'=>(@realpath($file) != $file) ? @realpath($file) : '',

'type_octal'=>sprintf("%07o", octdec($t)),
'is_readable'=> @is_readable($file),
'is_writable'=> @is_writable($file)
'device'=>$ss['dev'], //Device
'device_number'=>$ss['rdev'], //Device number, if device.
'inode'=>$ss['ino'], //File serial number
'link_count'=>$ss['nlink'], //link count
'link_to'=>($s['type']=='link') ? @readlink($file) : ''
'size'=>$ss['size'], //Size of file, in bytes.
'blocks'=>$ss['blocks'], //Number 512-byte blocks allocated
'block_size'=> $ss['blksize'] //Optimal block size for I/O.
'mtime'=>$ss['mtime'], //Time of last modification
'atime'=>$ss['atime'], //Time of last access.
'ctime'=>$ss['ctime'], //Time of last status change
'accessed'=>@date('Y M D H:i:s',$ss['atime']),
'modified'=>@date('Y M D H:i:s',$ss['mtime']),
'created'=>@date('Y M D H:i:s',$ss['ctime'])


|=---------[ Example Output ]

[perms] => Array
  [umask] => 0022
  [human] => -rw-r--r--
  [octal1] => 644
  [octal2] => 0644
  [decimal] => 100644
  [fileperms] => 33188
  [mode1] => 33188
  [mode2] => 33188
[filetype] => Array
  [type] => file
  [type_octal] => 0100000
  [is_file] => 1
  [is_dir] =>
  [is_link] =>
  [is_readable] => 1
  [is_writable] => 1
[owner] => Array
  [fileowner] => 035483
  [filegroup] => 23472
  [owner_name] => askapache
  [group_name] => grp22558
[file] => Array
  [filename] => /home/askapache/askapache-stat/htdocs/ok/g.php
  [realpath] =>
  [dirname] => /home/askapache/askapache-stat/htdocs/ok
  [basename] => g.php
[device] => Array
  [device] => 25
  [device_number] => 0
  [inode] => 92455020
  [link_count] => 1
  [link_to] =>
[size] => Array
  [size] => 2652
  [blocks] => 8
  [block_size] => 8192
[time] => Array
  [mtime] => 1227685253
  [atime] => 1227685138
  [ctime] => 1227685253
  [accessed] => 2008 Nov Tue 23:38:58
  [modified] => 2008 Nov Tue 23:40:53
  [created] => 2008 Nov Tue 23:40:53
antonixyz at gmx dot net 21-Jul-2008 02:19
= stat($filepath);
$mode = $stat[2];
is identical to:
<?php $mode = fileperms($filepath); ?>

at least on my linux box.
mail4rico at gmail dot com 24-Apr-2008 11:19
In response to the note whose first line is:
Re note posted by "admin at smitelli dot com"

I believe you have the conversion backwards. You should add an hour to filemtime if the system is in DST and the file is not. Conversely, you should subtract an hour if the file time is DST and the current OS time is not.

Here's a simplified, corrected version:
function getmodtime($file) { //returns the time a file was modified.
$mtime = filemtime($file);
//date('I') returns 1 if DST is on and 0 if off.
$diff = date('I')-date('I', $mtime);
//diff =  0 if file-time and os-time are both in the same DST setting
        //diff =  1 if os is DST and file is not
        //diff = -1 if file is DST and os is not
return $mtime + $diff*3600;
Here's a test:
//create two dummy files:
$file0 = 'file1.txt';
$file1 = 'file2.txt';
file_put_contents($file0, '');
file_put_contents($file1, '');
$time0=strtotime('Jan 1 2008 10:00'); echo 'Date0 (ST): ' . date(DATE_COOKIE, $time0)."\n";
$time1=strtotime('Aug 1 2008 10:00'); echo 'Date1 (DT): ' . date(DATE_COOKIE, $time1)."\n";
touch($file0, $time0); //set file0 to Winter (Non-DST)
touch($file1, $time1); //set file1 to Summer (DST)
$ftime0 = filemtime($file0);
$ftime1 = filemtime($file1);
"\nUncorrected: \n";
'File 0: ' . ($ftime0-$time0) ."\n";
'File 1: ' . ($ftime1-$time1) ."\n";
//if your system adjusts for DST, then _one_ of the above should be 3600 or -3600, depending on the time of year
$ftime0 = getmodtime($file0); //use filemtime correction
$ftime1 = getmodtime($file1); //use filemtime correction
echo "\nCorrected: \n";
'File 0: ' . ($ftime0-$time0) ."\n";
'File 1: ' . ($ftime1-$time1) ."\n";
//both of the corrected values output should be 0.

(when run in summer)
Date0 (ST): Tuesday, 01-Jan-08 10:00:00 EST
Date1 (DT): Friday, 01-Aug-08 10:00:00 EDT

File 0: -3600
File 1: 0

File 0: 0
File 1: 0
(when run in winter--dates omitted)
File 0: 0
File 1: 3600

File 0: 0
File 1: 0

In response to Re note posted by "admin at smitelli dot com",  your version below gives the following output when substituted into my test:
(when run in summer--dates omitted)
File 0: -3600
File 1: 0

File 0: -7200
File 1: 0
You can see that the operation is the opposite of what it should be.
sh at advisa dot fr 24-Apr-2008 05:35
Another possibility to get the whole dir size, using "du" on Linux

$size = exec("du -sm /your/path | awk '{print $1}'");
Hellhound 03-Mar-2008 02:16
To ignore index number or name specifics.. use:

list($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks)
         = lstat($directory_element);
JulieC 30-Jan-2007 07:21
The dir_size function provided by "marting.dc AT" works great, except the $mas variable is not initialized.  Add:

   $mas = 0;

before the while() loop.
piranha-php dot net at thoughtcrime dot us 16-Jul-2006 05:04
stat() returns a file's _status_, not its _statistics_.  "Statistics" implies information interpreted from the data of several files, not concrete meaning from a single file.  Both Linux and POSIX manual pages for stat() list the name as "stat - get file status," and do not mention the word "statistic" anywhere.
hugues dot larrive at gmail dot com 08-Jun-2006 01:49
salisbm at hotmail dot com said :
(...)to see if the file is a directory, after calling fstat, I do:
if ($fstats[mode] & 040000)
  ... this must be a directory

Then I say no no no no... it can be a directory or a named pipe, or a block sp├ęcial ...

The good code for this thing is :

if(($fstat['mode'] & 0170000) == 040000) echo "Be sure it is a directory !";

Sorry for very ugly english ;)
16-May-2006 10:10
Re note posted by "admin at smitelli dot com"

I'm not sure how that can work all year round since you have to modify both opposing inside and outside DST based on the actual files themselves, as well as the current DST setting for the system.

e.g. using filemtime, same thing for stat.


= filemtime($file);

if (
date('I') == 1) {
// Win DST is enabled, adjust standard time
    // files back to 'real' file UTC.
if (date('I', $mtime) == 0) {
$mtime -= 3600;
} else {
// Win DST is disabled, adjust daylight time
    // files forward to 'real' file UTC.
if (date('I', $mtime) == 1) {
$mtime += 3600;

gmdate('Y-m-d H:i:s', $mtime);


Just another example of why 'not' to use windows in a server room.
marting.dc AT 28-Jan-2006 05:08
If you want to know a directory size, this function will help you:

function dir_size($dir)
$handle = opendir($dir);
    while (
$file = readdir($handle)) {
        if (
$file != '..' && $file != '.' && !is_dir($dir.'/'.$file)) {
$mas += filesize($dir.'/'.$file);
            } else if (
is_dir($dir.'/'.$file) && $file != '..' && $file != '.') {
$mas += dir_size($dir.'/'.$file);
dir_size('DIRECTORIO').' Bytes';
admin at smitelli dot com 02-Nov-2005 11:27
There's an important (yet little-known) problem with file dates on Windows and Daylight Savings. This affects the 'atime' and 'mtime' elements returned by stat(), and it also affects other filesystem-related functions such as fileatime() and filemtime().

During the winter months (when Daylight Savings isn't in effect), Windows will report a certain timestamp for a given file. However, when summer comes and Daylight Savings starts, Windows will report a DIFFERENT timestamp! Even if the file hasn't been altered at all, Windows will shift every timestamp it reads forward one full hour during Daylight Savings.

This all stems from the fact that M$ decided to use a hackneyed method of tracking file dates to make sure there are no ambiguous times during the "repeated hour" when DST ends in October, maintain compatibility with older FAT partitions, etc. An excellent description of what/why this is can be found at

This is noteworthy because *nix platforms don't have this problem. This could introduce some hard-to-track bugs if you're trying to move scripts that track file timestamps between platforms.

I spent a fair amount of time trying to debug one of my own scripts that was suffering from this problem. I was storing file modification times in a MySQL table, then using that information to see which files had been altered since the last run of the script. After each Daylight Savings change, every single file the script saw was considered "changed" since the last run, since all the timestamps were off by +/- 3600 seconds.

This one-liner is probably one of the most incorrect fixes that could ever be devised, but it's worked flawlessly in production-grade environments... Assuming $file_date is a Unix timestamp you've just read from a file:

if (date('I') == 1) $file_date -= 3600;

That will ensure that the timestamp you're working with is always consistently reported, regardless of whether the machine is in Daylight Savings or not.
com dot gmail at algofoogle 21-Jul-2005 06:06
Re note posted by "salisbm at hotmail dot com":

S_IFDIR is not a single-bit flag. It is a constant that relies on the "S_IFMT" bitmask. This bitmask should be applied to the "mode" parameter before comparing with any of the other "S_IF..." constants, as indicated by stat.h:

#define S_ISDIR(m)  (((m) & S_IFMT) == S_IFDIR)

That is, this approach is incorrect:

if (
$mode & S_IFDIR)
    format could be S_IFDIR, but also

...and should instead be:

if (
S_IFDIR == ($mode & S_IFMT)) {  /* ... */  }

As pointed out by "svend at svendtofte dot com", however, there is also the "is_dir" function for this purpose, along with "is_file" and "is_link" to cover the most common format types...
mpb dot mail at gmail dot com 17-Jul-2005 03:10
If you are working with files larger than 2GB (and PHP's integer type is only 32 bits on your system) then you can try the following to get floating point sizes:

On FreeBSD:

$size = (float) exec ('stat -f %z '. escapeshellarg ($path));

On Linux:

$size = (float) exec ('stat -c %s '. escapeshellarg ($path));

(The other example that uses "ls" and "awk" does not properly escape the filename, but should work otherwise.)
mao at nospam dot com 07-Jun-2005 05:53
If you have ftp (and the related sftp) protocols disabled on your remote server, it can be hard figuring out how to 'stat' a remote file. The following works for me:


= ssh2_connect($host, 22);
ssh2_auth_password($conn, $user, $password);
$stream = ssh2_exec($conn, "stat $fileName > $remotedest");
ssh2_scp_recv($conn, $remotedest, $localdest);
$farray = file($localdest);
guillermo martinez 30-Jan-2005 10:24
stat() and SELinux,

You can have troubles to use the stat() function if the SELinux is enabled, so check the SELinux documentation or turn it off.
10-Nov-2004 07:41
If the 2GB limit is driving you crazy, you can use this complete hack.  use in place of filesize()

function file_size($file) {
  $size = filesize($file);
  if ( $size == 0)
    $size = exec("ls -l $file | awk '{print $5}'");
  return $size;
svend at svendtofte dot com 09-Oct-2004 04:31
To the note of how you can figure out if a file is a folder or not, there is also the handy "is_dir" function.
salisbm at hotmail dot com 11-Aug-2003 05:21
I was curious how I could tell if a file was a directory... so I found on the following information about the mode bits:
#define S_IFMT 0170000           /* type of file */
#define        S_IFIFO  0010000  /* named pipe (fifo) */
#define        S_IFCHR  0020000  /* character special */
#define        S_IFDIR  0040000  /* directory */
#define        S_IFBLK  0060000  /* block special */
#define        S_IFREG  0100000  /* regular */
#define        S_IFLNK  0120000  /* symbolic link */
#define        S_IFSOCK 0140000  /* socket */
#define        S_IFWHT  0160000  /* whiteout */
#define S_ISUID 0004000  /* set user id on execution */
#define S_ISGID 0002000  /* set group id on execution */
#define S_ISVTX 0001000  /* save swapped text even after use */
#define S_IRUSR 0000400  /* read permission, owner */
#define S_IWUSR 0000200  /* write permission, owner */
#define S_IXUSR 0000100  /* execute/search permission, owner */

Note that these numbers are in octal format.  Then, to check to see if the file is a directory, after calling fstat, I do:

if ($fstats[mode] & 040000)
  ... this must be a directory
ian at eiloart dot com 23-Jul-1999 08:52
Here's what the UNIX man page on stat has to say about the difference between a file change and  a file modification:

st_mtime  Time when data was last modified.  Changed by  the following  functions:   creat(),  mknod(), pipe(), utime(), and write(2).

st_ctime  Time when file status was last  changed.   Changed by  the  following  functions: chmod(), chown(), creat(), link(2), mknod(), pipe(), unlink(2), utime(), and write().

So a modification is a change in the data, whereas a change also happens if you modify file permissions and so on.

show source | credits | sitemap | contact | advertising | mirror sites