2024 年 PHP 日本會議

filectime

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

filectime取得檔案的 inode 變更時間

說明

filectime(字串 $filename): 整數|false

取得檔案的 inode 變更時間。

參數

filename

檔案的路徑。

回傳值

返回檔案最後變更的時間,失敗則返回 false。時間以 Unix 時間戳記的形式返回。

錯誤/例外

失敗時,會發出 E_WARNING 錯誤訊息。

範例

範例 #1 filectime() 範例

<?php

// 輸出例如 somefile.txt 最後變更時間:2002 年 12 月 29 日 22:16:23。

$filename = 'somefile.txt';
if (
file_exists($filename)) {
echo
"$filename 最後變更時間: " . date("F d Y H:i:s.", filectime($filename));
}

?>

注意事項

注意:

注意:在大部分的 Unix 檔案系統中,當檔案的 inode 資料變更時,檔案就被視為已變更;也就是 inode 中的權限、擁有者、群組或其他中繼資料更新時。另請參閱 filemtime()(當您想要在網頁上建立「上次修改」頁尾時,應該使用這個函式)以及 fileatime()

注意:

另請注意,在某些 Unix 文件中,檔案的 ctime 被稱為檔案的建立時間。這是錯誤的。在大部分的 Unix 檔案系統中,Unix 檔案沒有建立時間。

注意:

請注意,時間解析度可能因檔案系統而異。

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

提示

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

另請參閱

新增註解

使用者貢獻的註解 9 則註解

StevieMc at example dot com
18 年前
這個方法會取得目錄中的所有檔案,並按照它們新增(透過 ftp 或其他方式)的日期順序顯示它們。

<?PHP
function dirList ($directory, $sortOrder){

//取得每個檔案並將其詳細資訊新增到兩個陣列中
$results = array();
$handler = opendir($directory);
while (
$file = readdir($handler)) {
if (
$file != '.' && $file != '..' && $file != "robots.txt" && $file != ".htaccess"){
$currentModified = filectime($directory."/".$file);
$file_names[] = $file;
$file_dates[] = $currentModified;
}
}
closedir($handler);

//依據指定的順序排序日期陣列
if ($sortOrder == "newestFirst"){
arsort($file_dates);
}else{
asort($file_dates);
}

//將 file_names 陣列與 file_dates 陣列配對
$file_names_Array = array_keys($file_dates);
foreach (
$file_names_Array as $idx => $name) $name=$file_names[$name];
$file_dates = array_merge($file_dates);

$i = 0;

//迴圈處理日期陣列,然後輸出列表
foreach ($file_dates as $file_dates){
$date = $file_dates;
$j = $file_names_Array[$i];
$file = $file_names[$j];
$i++;

echo
"檔案名稱: $file - 新增日期: $date. <br/>"";
}

}
?>

希望這對某些人有用。
faketruth at yandex dot ru
9 年前
如果您需要在 Mac OS X 上取得檔案建立時間

<?php
if ($handle = popen('stat -f %B ' . escapeshellarg($filename), 'r')) {
$btime = trim(fread($handle, 100));
echo
strftime("btime: %Y.%m.%d %H:%M:%S\n", $btime);
pclose($handle);
}
?>
javi at live dot com
15 年前
Filemtime 函式似乎會回傳資料夾內最早修改檔案的日期,所以這是一個遞迴函式,用於回傳資料夾內最後(最近)修改檔案的日期。

<?php

// 只考慮您想要顯示其副檔名的檔案。
$allowedExtensions = array(
'zip',
'rar',
'pdf',
'txt'
);

function
filemtime_r($path)
{
global
$allowedExtensions;

if (!
file_exists($path))
return
0;

$extension = end(explode(".", $path));
if (
is_file($path) && in_array($extension, $allowedExtensions))
return
filemtime($path);
$ret = 0;

foreach (
glob($path."/*") as $fn)
{
if (
filemtime_r($fn) > $ret)
$ret = filemtime_r($fn);
// 這將回傳一個時間戳記,您必須使用 date() 函式來格式化它。
}
return
$ret;
}

?>
coolkoon at gmail dot com
13 年前
您應該避免將沒有路徑的檔案傳遞給函式。這適用於 filemtime(),也可能適用於 fileatime()。如果您省略路徑,該指令將會失敗,並顯示警告「filectime(): stat failed for filename.php」。
website at us dot kaspersky dot com
17 年前
上面程式碼的第 37 行有一個錯誤。

echo "File name: $file - Date Added: $date. <br/>"";

<br/> 後面有一個多餘的 " 需要刪除,程式碼才能正常運作。
m dot rabe at directbox dot com
14 年前
在 Windows 系統下,您可以使用 fileatime() 取代 filectime()。
soapergem at gmail dot com
15 年前
請注意,在 Windows 系統上,filectime() 會顯示檔案的建立時間,因為 Windows 系統中沒有「變更時間」的概念。
chuck dot reeves at gmail dot com
17 年前
在 Windows 系統上執行 filectime() 讀取 Samba 共享上的檔案時,仍會顯示上次修改日期。
laurent dot pireyn at wanadoo dot be
23 年前
如果您對 symbolic link 使用 filectime(),您會得到實際連結到的檔案的變更時間。要取得連結本身的資訊,請使用 lstat()。
To Top