Tutorialspoint.dev

PHP | lstat( ) function

The lstat() function in PHP is used to return information about a file or a symbolic link. It gathers statistics of the file which is sent as a parameter to the lstat() function. The function returns an array which includes information on 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 (if supported)
  • [12] or [blocks] – Number of blocks allocated

Note:

This function is similar to stat(), except that if the file parameter is a symbolic link, the status of the symlink is return not the status of the file pointed to by the symlink.

Syntax:

lstat(file)

Parameters Used:
The lstat() function in PHP accepts one parameter.



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

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

Exceptions:

  1. The results of the lstat() function differs from server to server.
  2. The result of this function are cached and therefore the clearstatcache() function is used to clear the cache.
  3. An E_WARNING is emitted on failure.

Example: 1

Input : print_r(lstat("gfg.txt"));

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
)

Example: 2

Input : symlink('gfg.php', 'gfg');
        array_diff(stat('gfg'), lstat('gfg'));
Output :
Array
(
    [ino] => 97236376
    [mode] => 33188
    [size] => 34
    [atime] => 1223580003
    [mtime] => 1223581848
    [ctime] => 1223581848
    [blocks] => 8
)

Explanation: Difference of the resluts of stat() and lstat() function

Below programs illustrate the lstat() function.

Program 1

<?php 
// displaying information using lstat() function
print_r(lstat("gfg.txt"));
?>

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 
  
// creating a symbolic link
symlink('gfg.php', 'gfg');
  
// comparing information returned
//  by stat() and lstat() function
array_diff(stat('gfg'), lstat('gfg'));
?>

Output:

Array
(
    [ino] => 97236376
    [mode] => 33188
    [size] => 34
    [atime] => 1223580003
    [mtime] => 1223581848
    [ctime] => 1223581848
    [blocks] => 8
)

Program 3

<?php 
// displaying information of 
// zip file using lstat() function
$myfile = lstat("./gfg.zip");
echo($myfile);
?>

Output:

Array (
[0] => 2161 
[1] => 18351063 
[2] => 33188 
[3] => 1 
[4] => 1036 
[5] => 1036 
[6] => 0 
[7] => 270081 
[8] => 1382409024 
[9] => 1382409631 
[10] => 1382409631 
[11] => 4096 
[12] => 528
[dev] => 2161 
[ino] => 18351063 
[mode] => 33188 
[nlink] => 1 
[uid] => 1036 
[gid] => 1036 
[rdev] => 0 
[size] => 270081 
[atime] => 1382409024 
[mtime] => 1382409631 
[ctime] => 1382409631 
[blksize] => 4096 
[blocks] => 528 )

Related Article: PHP | stat( ) function

Reference:
http://php.net/manual/en/function.lstat.php



This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter