PHP: How to get the creation time of a file or directory

Updated: January 11, 2024 By: Guest Contributor Post a comment

Introduction

In PHP, managing files and directories is a common task performed by developers. Amongst other things, you may need to determine the creation time of a file or a directory to manage uploads, backups, or simple file operations. This tutorial will walk you through the various ways to retrieve the creation time also known as the inode change time of a file or directory in PHP.

Simple File Operations in PHP

Before we delve into getting the creation time of files and directories, it’s important to ensure that you are familiar with the PHP filesystem functions. PHP offers a variety of built-in functions that allow you to handle files and directories effectively. You can create, delete, modify and perform other operations using these functions.

One such useful function provided is filemtime() which is used to get the last modification time of a file. Since PHP does not have a straightforward way to get the exact creation time of a file, sometimes the last modification time can be used as a close estimate.

Using filemtime() to Get Last Modified Time

<?php
// Assume 'example.txt' is a file in your directory
$filename = 'example.txt';

if (file_exists($filename)) {
    echo "The last modification time of $filename was: " . date("F d Y H:i:s.", filemtime($filename));
} else {
    echo "The file does not exist.";
}
?>

filemtime() returns the last modification time of the file. If you know for sure that the file was never modified after being uploaded or created, then the modification time is in fact the creation time.

Using stat() for File Metadata

The stat() function gives you an associative array with all the statistics of a files or folders, including the modification time.

<?php
$filename = 'example.txt';
$stat = stat($filename);
if($stat) {
    echo "Creation time (inode change time): " . date("F d Y H:i:s.", $stat['ctime']);
}
?>

Note that ‘ctime’, here does not strictly refer to the ‘creation time’ as in other operating systems but refers to the ‘inode change time’. This value will be updated not only when the file is created but also when it’s metadata or contents are changed.

How filectime() Works

filectime() is another PHP function that is similar to stat() in functionality but instead of an array, it provides directly the timestamp representing the inode change time of the file.

<?php
$filename = 'example.txt';

if (file_exists($filename)) {
    echo "The inode change time of $filename is: " . date("F d Y H:i:s.", filectime($filename));
} else {
    echo "The file does not exist.";
}
?>

This will yield the same inode change time that stat() would report. Remember this is the closest you can get to the ‘creation time’ of a file using standard PHP functions.

Platform Specific Solutions

One way to truly get the creation time of a file is to use system-specific commands through PHP’s exec() function. The approach is different for Linux and Windows systems:

For Linux-based Systems

Linux does not commonly store the creation time of files, but offers the ‘birth’ time which can be retrieved using the ‘stat‘ command. It is important to note that not every file system supports recording the birth time.

<?php
$filename = 'example.txt';

$command = "stat -c %w $filename";
$output = exec($command, $output, $return_var);
if($return_var === 0) {
    echo "The creation (birth) time of $filename is: $output";
} else {
    echo "Command failed with status: $return_var";
}
?>

For Windows Systems

Windows does track the creation time and can be easily fetched using ‘dir‘ in Command Prompt.

<?php
$filename = 'C:\path\to\example.txt';

$command = "dir /T:C "$filename"";
$output = exec($command, $output, $return_var);
if($return_var === 0 && isset($output[5])) {
   preg_match('/(\d{2}\/\d{2}\/\d{4}\s+\d{1,2}:\d{2}\s+(AM|PM))/', $output[5], $matches);
    echo "The creation time of $filename is: " . $matches[1];
} else {
    echo "Failed to find the creation date.";
}
?>

Note that handling outputs and return values properly is important for these shell commands to retrieve file creation times in PHP.

Conclusion

Throughout this tutorial, we have seen how to get the creation time of a file or directory using PHP. We started off with PHP built-in functions like filemtime(), stat(), and filectime() which provide times close to what we need. Then we explained how to use system-specific commands in order to get more precise creation times on different operating systems.

When dealing with filesystems and PHP, always ensure that you have the necessary permissions to access and modify the files. Also, keep in mind that PHP’s internal functions may sometimes behave differently based on the underlying operating system’s handling of file metadata.

Determining a file’s creation time is just one aspect of a robust file management system in PHP, which includes handling permissions, manipulation, and other filesystem operations. Use these techniques judiciously to build more informed and capable applications that handle files and directories with precision.