2024 年 PHP Conference Japan

lstat

(PHP 4, PHP 5, PHP 7, PHP 8)

lstat取得檔案或符號連結的資訊

說明

lstat(字串 $filename): 陣列|false

收集由 filename 指定的檔案或符號連結的統計資訊。

參數

filename

檔案或符號連結的路徑。

回傳值

關於 lstat() 所返回陣列的結構資訊,請參閱 stat() 的手冊頁面。此函式與 stat() 函式相同,但如果 filename 參數是符號連結,則返回的是符號連結的狀態,而不是符號連結指向的檔案的狀態。

如果失敗,將返回 false

錯誤/例外

失敗時,會發出 E_WARNING 錯誤。

範例

範例 #1 比較 stat()lstat()

<?php
symlink
('uploads.php', 'uploads');

// 對比 uploads.php 和 uploads 的資訊
array_diff(stat('uploads'), lstat('uploads'));
?>

上述範例將輸出類似以下的內容

兩個檔案之間不同的資訊。

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

注意事項

注意此函式的結果會被快取。詳情請參閱 clearstatcache()

提示

從 PHP 5.0.0 開始,此函式也可以與某些 URL 包裝器一起使用。請參閱 支援的協定和包裝器 以確定哪些包裝器支援 stat() 系列函式。

另請參閱

新增筆記

使用者貢獻的筆記 2 則筆記

-2
HP@SG
4 年前
僅供參考,並回覆 4 年前 "salsi at icosaedro dot it" 留言

在 64 位元 Linux 系統上可以處理大於 2 GiB 的檔案。

我的終端機測試如下(使用 <?php ;?> 標籤為結果著色以便於閱讀)

$ php -v

<?php
"
PHP 7.2.24-0ubuntu0.18.04.7 (cli) (built: Oct 7 2020 15:24:25) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.24-0ubuntu0.18.04.7, Copyright (c) 1999-2018, by Zend Technologies
"
;?>

$ date ; dd if=/dev/zero of=/tmp/php_test_huge bs=1024K count=2100 ; date ; ls -l /tmp/php_test_huge

<?php
"
2020年11月11日 週三 15:35:46 +08
2100+0 records in
2100+0 records out
2202009600 bytes (2.2 GB, 2.1 GiB) copied, 4.79192 s, 460 MB/s
2020年11月11日 週三 15:35:51 +08
-rw-r--r-- 1 harold harold 2202009600 11月 11 15:35 /tmp/php_test_huge
"
;?>

$ php -r 'var_dump(lstat("/tmp/php_test_huge"));'

<?php
"
array(26) {
[0]=>
int(2050)
[1]=>
int(19923027)
[2]=>
int(33188)
[3]=>
int(1)
[4]=>
int(1000)
[5]=>
int(1000)
[6]=>
int(0)
[7]=>
int(2202009600)
[8]=>
int(1605079647)
[9]=>
int(1605080149)
[10]=>
int(1605080149)
[11]=>
int(4096)
[12]=>
int(4300808)
["
dev"]=>
int(2050)
["
ino"]=>
int(19923027)
["
mode"]=>
int(33188)
["
nlink"]=>
int(1)
["
uid"]=>
int(1000)
["
gid"]=>
int(1000)
["
rdev"]=>
int(0)
["
size"]=>
int(2202009600)
["
atime"]=>
int(1605079647)
["
mtime"]=>
int(1605080149)
["
ctime"]=>
int(1605080149)
["
blksize"]=>
int(4096)
["
blocks"]=>
int(4300808)
}
"
;?>
-3
salsi at icosaedro dot it
8 年前
此函式在 32 位元 Linux 系統 (PHP 7.1.0-dev) 上處理超過 2 GB 的檔案時會失敗並返回 FALSE。

$ dd if=/dev/zero of=/tmp/huge bs=1048576 count=2050
$ php -r 'var_dump(lstat("/tmp/huge"));'
--> 警告: lstat(): Lstat failed for /tmp/huge in Command line code on line 1

尚未在 Windows 上測試。尚未在 64 位元 PHP 上測試。
To Top