Current Location: Home> Latest Articles> How to Get a File’s Inode Number Using PHP’s fileinode Function: Steps and Examples

How to Get a File’s Inode Number Using PHP’s fileinode Function: Steps and Examples

gitbox 2025-07-10

1. What is an inode?

In a file system, each file has a unique inode that contains various information about the file, such as its size, permissions, owner, file type, and more. The inode itself does not contain the filename; the link between the filename and inode is established through directory entries.

Simply put, an inode is the “ID card” of a file, helping the file system manage files.

2. Overview of PHP’s fileinode() Function

fileinode() is a built-in PHP function used to retrieve the inode number of a specified file. Its syntax is as follows:

<span><span><span class="hljs-title function_ invoke__">fileinode</span></span><span>(</span><span><span class="hljs-keyword">string</span></span><span> </span><span><span class="hljs-variable">$filename</span></span><span>): </span><span><span class="hljs-keyword">int</span></span><span>|</span><span><span class="hljs-literal">false</span></span><span>
</span></span>
  • $filename: The path to the file. The file must exist; otherwise, the function returns false.

  • Return value:

    • Returns the inode number of the file as an integer.

    • If the file does not exist or cannot be accessed, returns false.

3. How to Use fileinode() to Get a File’s Inode Number

Using PHP’s fileinode() function, you can easily obtain a file’s inode number. Here are the steps:

Step 1: Confirm the File Exists

First, verify that the specified file actually exists. If it does not, the fileinode() function will return false.

Step 2: Call the fileinode() Function

Pass the file path to fileinode() to get the inode number of that file.

Step 3: Handle the Return Value

If the file exists, fileinode() returns an integer representing the inode number; if the file does not exist or is inaccessible, it returns false.

4. Example Code

Here is a complete example demonstrating how to use fileinode() to get a file’s inode number:

<span><span><span class="hljs-meta">&lt;?php</span></span><span>
</span><span><span class="hljs-comment">// Set the file path</span></span><span>
</span><span><span class="hljs-variable">$file_path</span></span><span> = </span><span><span class="hljs-string">&#039;example.txt&#039;</span></span>;
<p></span>// Check if the file exists<br>
if (file_exists($file_path)) {<br>
// Get the inode number of the file<br>
$inode = fileinode($file_path);</p>
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"The inode number of the file is: "</span></span><span> . </span><span><span class="hljs-variable">$inode</span></span><span>;

} else {
echo "File does not exist.";
}
?>

Explanation:

  1. The file_exists() function checks if the file exists.

  2. If the file exists, fileinode() returns the inode number, which is then printed.

  3. If the file does not exist, it outputs "File does not exist."

5. Example Output

Assuming we have a file named example.txt located in the current directory, running the above PHP code will output something like:

<span><span>The inode number of the file is: 123456
</span></span>

Here, 123456 is just an example; the actual inode number will vary depending on your file system.

6. Notes

  • File Permissions: Ensure the PHP script has sufficient permissions to read the file. If permissions are lacking, fileinode() won’t be able to retrieve the inode number.

  • Symbolic Links: If the file is a symbolic link (symlink), fileinode() returns the inode number of the symlink itself, not the inode number of the file it points to. To get the inode number of the actual file the symlink references, you can use the realpath() function to get the real path.

<span><span><span class="hljs-variable">$file_path</span></span><span> = </span><span><span class="hljs-string">&#039;symlink.txt&#039;</span></span><span>;
</span><span><span class="hljs-variable">$real_path</span></span><span> = </span><span><span class="hljs-title function_ invoke__">realpath</span></span><span>(</span><span><span class="hljs-variable">$file_path</span></span><span>);
</span><span><span class="hljs-keyword">if</span></span><span> (</span><span><span class="hljs-variable">$real_path</span></span><span>) {
    </span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"The inode number of the file pointed to by the symlink is: "</span></span><span> . </span><span><span class="hljs-title function_ invoke__">fileinode</span></span><span>(</span><span><span class="hljs-variable">$real_path</span></span><span>);
}
</span></span>
  • Return value false: If the file does not exist or the path is incorrect, fileinode() returns false. You can use the is_file() function to further verify if the path points to a valid file.

7. Conclusion

Using PHP’s fileinode() function, you can conveniently obtain the inode number of a specified file. This can be useful in certain scenarios such as uniquely identifying files or managing the file system. As long as the file exists and the PHP script has sufficient permissions, you can retrieve the inode number without issues.