Hello all.
I was looking for a way to make some grafical stats of recieved files on my honeypot.
After thinking on the way to do so, i understood that the easiest way would be parsing the filename with regullar expression or using dir() command.
But for parsing the data i didnt find a way to do it with shell or directly with php, and thought that "ls -l" outputs the creation date, all fine till that point, but the problem arises here, i was thinking on parsing the date as yyyy-mm-dd, but when i do "ls -l" in php the output is given as "Sep 19" for example.
So is there a way in php to see the creation date of the file or to output directly the command from the linux so it will show "yyyy-mm-dd" .
Thanks in advance.

Dani AI

Generated

Quick summary and practical next steps that build on and without reusing their exact examples.

ls -l is a poor source to parse because its display is intentionally compact: recent files show a month+day/time, older files show a year. That explains the "Sep 19" you saw. Rather than regexing ls output, use tools that emit stable timestamps.

You can force ls to show a full date string or ask the OS for the file's birth time when available. For example, GNU ls supports --time-style to control the printed format, and GNU stat can print a file's birth time (if the filesystem/kernel exposes it). See the GNU coreutils docs for ls and stat for accepted format options (ls docs, stat docs).

Important portability notes: on Unix/Linux, a true "creation" timestamp may not exist for every filesystem. PHP’s filectime() is often misunderstood — on Unix it is the inode change time (not creation), while on Windows it represents creation time; see the PHP docs for details (PHP filectime manual). Newer Linux kernels expose file birth time via statx when the filesystem supports it; see the statx man page for how that field is reported (statx).

Practical strategy: (1) check if birth time is available with stat; (2) if not, fall back to modification time; (3) for reliable long-term records, record timestamps at the moment your honeypot process saves the file (or use inotify to capture creation events) and store that metadata in a database. The inotify facility is a lightweight way to catch actual creation events on Linux (inotify docs).

Recommended Answers

All 4 Replies

It's all in the date command.

Just execute this from shell:

$ date +%Y-%m-%d

Or in your bash script:
FormattedDate=$(date +%Y-%m-%d)
echo $FormattedDate

of course.. the way to your holy grail is

$ man date

The ideia was to see the date of file created. And not the current date.

You can get the file modification time in PHP:

http://www.php.net/filemtime

$mtime = filemtime('/path/to/file');

To get the files in the directory, you can use glob()
http://us.php.net/manual/en/function.glob.php

Or with PHP5, use RecursiveDirectoryIterator class.

eg:

$dir_iterator = new RecursiveDirectoryIterator("/path");
$iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST);
// could use CHILD_FIRST if you so wish

foreach ($iterator as $file) {
    echo $file->getMTime()."\n";
}

see: http://us.php.net/manual/en/function.glob.php#92710

When you use the RecursiveDirectoryIterator class, each file becomes an instance of: SplFileInfo class.

So you can access the file using the methods defined for SplFileInfo.
http://us3.php.net/manual/en/class.splfileinfo.php

It's all in the date command.

Just execute this from shell:

$ date +%Y-%m-%d

Or in your bash script:
FormattedDate=$(date +%Y-%m-%d)
echo $FormattedDate

of course.. the way to your holy grail is

$ man date

The date command also accepts a file path using the -r option, taking its last modification time as the input date.

eg:

FilePath=/path/to/file
FormattedDate=$(date -r $FilePath +%Y-%m-%d)
echo $FormattedDate

You could also parse the date out of the ls -l with sed, awk, grep etc. and pass that to: date -d

Ok, will use the date, and will try to read man's more intensivly.
Thanks for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.