PHP fileinode() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, fileinode() returns the inode number of a file. An inode is the internal record that a Unix style file system uses to keep track of a file.

Every file on the disk has its own inode number. Two paths that point to the same inode are really the same file, which makes fileinode() useful for spotting hard links.

Syntax

Call fileinode() with the values you need. Start with this line.

PHP
$inode = fileinode('notes.txt');
CallWhat it does
fileinode('notes.txt')Returns the inode number of notes.txt, for example 2491873.
fileinode(__DIR__)Works on folders too and returns the inode of the directory.
fileinode('missing.txt')Returns false and raises a warning because the file does not exist.

On Windows, fileinode() does not give a useful number and often returns 0, because NTFS does not use inodes in the same way. Results are also cached, so call clearstatcache() if the file changes during the script.

Library declarations
PHP
fileinode(string $filename): int|false

Parameters

ParameterDescription
filenameRequired. The path to the file or folder you want to check.

Return Value

Return ValueDescription
intReturns the inode number of the file on success.
falseReturns false on failure, for example when the file does not exist.

PHP Examples

Example 1 — Read the Inode of a File

Create a small file and print its inode number.

PHP
<?php
$file = __DIR__ . '/inode_demo.txt';
file_put_contents($file, 'Hello');

$inode = fileinode($file);

echo "Inode number: $inode", PHP_EOL;

unlink($file);

How It Works

  1. file_put_contents() creates a small text file next to the script.
  2. fileinode() asks the file system for the inode number of that file.
  3. The number is saved in $inode and printed. Your number will be different.
  4. unlink() deletes the demo file so nothing is left behind.

Example 2 — Handle a Missing File

fileinode() returns false when the path does not exist, so test the result before you use it.

PHP
<?php
$file = __DIR__ . '/missing.txt';

$inode = @fileinode($file);

if ($inode === false) {
    echo "Could not read the inode", PHP_EOL;
} else {
    echo "Inode: $inode", PHP_EOL;
}

How It Works

  1. The file missing. txt does not exist in the folder.
  2. fileinode() fails and returns false. The @ sign hides the PHP warning.
  3. === false checks the result without confusing it with the number 0.
  4. The script prints a friendly message instead of a warning.

Example 3 — Compare a File with Its Copy

A copy is a brand new file, so it gets a different inode from the original.

PHP
<?php
$a = __DIR__ . '/original.txt';
$b = __DIR__ . '/copy.txt';

file_put_contents($a, 'data');
copy($a, $b);

echo 'Same file twice: ', fileinode($a) === fileinode($a) ? 'yes' : 'no', PHP_EOL;
echo 'Original vs copy: ', fileinode($a) === fileinode($b) ? 'same inode' : 'different inode', PHP_EOL;

unlink($a);
unlink($b);

How It Works

  1. The script creates original. txt and then copies it to copy.txt.
  2. Reading the inode of the same path twice always gives the same number.
  3. The copy is a separate file, so its inode is different.
  4. A hard link made with link() would share the inode of the original.

Frequently Asked Questions

It returns the inode number of a file or folder. The inode is the unique id the file system uses internally for that item.
It returns false and raises an E_WARNING, for example when the file does not exist. Use === false to test for it.
Windows file systems do not use inodes the same way Unix systems do, so PHP often reports 0. Use it mainly on Linux and macOS.
On the same disk, only hard links share an inode. Files on different disks can have the same number, so do not compare inodes across disks.
PHP caches file information. Call clearstatcache() before fileinode() if the file was changed or replaced earlier in the script.

Next: filemtime()

filemtime() returns the time a file was last modified, as a Unix timestamp.

filemtime() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful