PHP Function Reference

PHP lstat() Function



The PHP lstat() function returns information (statistics) about a file or symbolic link specified by filename.

The results from this function will differ from server to server. The array may contain the number index, the name index, or both.

This function is identical to the stat() function except that if the filename parameter is a symbolic link, the status of the symbolic link is returned, not the status of the file pointed to by the symbolic link.

Note: The results of this function are cached. Use clearstatcache() function to clear the cache.

Syntax

lstat(filename)

Parameters

filename Required. Specify path to a file or a symbolic link.

Return Value

In case of error, returns false, otherwise 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 value of mode contains information read by several functions. When written in octal, starting from the right, the first three digits are returned by chmod(). The next digit is ignored by PHP. The next two digits indicate the file type. For example a regular file could be 0100644 and a directory could be 0040755.

mode file types

mode in octalDescription
0140000socket
0120000link
0100000regular file
0060000block device
0040000directory
0020000character device
0010000fifo

Note: As 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.

Exceptions

Upon failure, an E_WARNING is emitted.

Example: lstat() example

Lets assume that we have a file called test.txt. In the example below a symbolic link of this file is created. Then, the lstat() function is used to get the statistics about this symlink.

<?php
$target = 'test.txt';
$link = 'sampleTest';

symlink($target, $link);

//using the lstat() function to
//get statistics about the symlink
$info = lstat($link);

//displaying the statistics
print_r($info);
?>

The output of the above code will be:

Array
(
    [0] => 2049
    [1] => 523024
    [2] => 41471
    [3] => 1
    [4] => 33
    [5] => 33
    [6] => 0
    [7] => 8
    [8] => 1633622633
    [9] => 1633622633
    [10] => 1633622633
    [11] => 4096
    [12] => 0
    [dev] => 2049
    [ino] => 523024
    [mode] => 41471
    [nlink] => 1
    [uid] => 33
    [gid] => 33
    [rdev] => 0
    [size] => 8
    [atime] => 1633622633
    [mtime] => 1633622633
    [ctime] => 1633622633
    [blksize] => 4096
    [blocks] => 0
)

Example: using lstat() information together with touch() function

Consider the example below, where this function is used with touch() function.

<?php
$target = 'test.txt';
$link = 'sampleTest';

symlink($target, $link);

//using the lstat() function to
//get statistics about the symlink
$info = lstat($link);

//changing the access time to 5 hours in the past
$atime = $info['atime'] + 18000;
  
//using touch() function to change the access time
if (touch($link, time(), $atime)) {
  echo 'Access time changed to 5 hours in the past!';
} else {
  echo 'Access time could not be changed.';
}
?>

The output of the above code will be:

Access time changed to 5 hours in the past!

Example: comparison of stat() and lstat()

Consider one more example, where this function is compared with stat() function.

<?php
$target = 'test.txt';
$link = 'sampleTest';

symlink($target, $link);

//comparing stat() and lstat() function
print_r(array_diff(stat($target), lstat($link)));
?>

The output of the above code will be:

Array
(
    [1] => 521643
    [2] => 33188
    [7] => 48
    [ino] => 521643
    [mode] => 33188
    [size] => 48
)

❮ PHP Filesystem Reference