PHP 檔案系統 fileinode() 函式



PHP 檔案系統fileinode() 函式可以返回指定檔案的 inode。此函式在成功時返回檔案的 inode 號,在失敗時返回 false。

此函式的結果可以被快取,我們可以使用 clearstatcache() 函式來清除快取。此函式無法在 Windows 系統上執行。

inode 是檔案系統上的一個數據結構,它儲存有關檔案或目錄的資訊。

語法

以下是 PHP 檔案系統fileinode() 函式的語法 -

int fileinode ( string $filename )
序號 引數及描述
1

filename(必需)

要檢查的檔案的路徑。

返回值

它在成功時返回檔案的 inode 號,在失敗時返回 FALSE。

PHP 版本

fileinode() 函式首次作為核心 PHP 4 的一部分引入,並且與 PHP 5、PHP 7、PHP 8 相容良好。

示例

在下面的示例中,我們將展示 PHP 檔案系統fileinode() 函式的基本用法以獲取檔案的 inode 號。

<?php
   //File path
   $filename = "/PhpProject/sample.txt";
   
   //Use fileinode() function for the file
   echo fileinode($filename);
?>

輸出

這將產生以下結果 -

12666373952223775

示例

此示例向我們展示瞭如何獲取位於當前目錄中的檔案的 inode 號。因此,程式碼列印 inode 號或如果找不到檔案則列印錯誤訊息。

<?php
   $filename = 'myfile.txt';
   $inode = fileinode($filename);

   if ($inode !== false) {
      echo "The inode number of 'myfile.txt' is: $inode";
   } else {
      echo "Could not retrieve inode number for 'myfile.txt'.";
   }
?> 

輸出

這將生成以下結果 -

The inode number of 'myfile.txt' is: 11033122

示例

此 PHP 示例向我們展示瞭如何使用fileinode() 函式使用絕對路徑獲取檔案的 inode 號。

<?php
   // Absolute path to the file
   $filename = '/Applications/XAMPP/xamppfiles/htdocs/mac/myfile.txt';
   $inode = fileinode($filename);

   if ($inode !== false) {
      echo "The inode number of '/Applications/XAMPP/xamppfiles/htdocs/mac/myfile.txt' is: $inode";
   } else {
      echo "Could not retrieve inode number for '/Applications/XAMPP/xamppfiles/htdocs/mac/myfile.txt'.";
   }
?> 

輸出

這將建立以下結果 -

The inode number of '/Applications/XAMPP/xamppfiles/htdocs/mac/myfile.txt' is: 11033122

示例

此 PHP 示例向我們展示瞭如何使用fileinode() 函式獲取目錄而不是檔案的 inode 號。

<?php
   // Path to the directory
   $directory = '/var/www/html';
   $inode = fileinode($directory);

   if ($inode !== false) {
      echo "The inode number of the directory '/var/www/html' is: $inode";
   } else {
      echo "Could not retrieve inode number for the directory '/var/www/html'.";
   }
?> 

輸出

這將導致以下結果 -

The inode number of the directory '/var/www/html' is: 10105201

注意

  • fileinode() 函式可用於獲取檔案和目錄的 inode 號。
  • inode 號在檔案系統中是唯一的。不同檔案系統上檔案的 inode 計數可能相同。
  • 驗證您是否具有訪問檔案或目錄所需的許可權。

總結

PHP 中的fileinode() 函式是確定檔案或目錄的 inode 號的有用工具。

php_function_reference.htm
廣告