WGS
WGS

Reputation: 199

Find files in folders and subfolders that were created on the current date

I am trying to find files that were created today. I found most of my answer in other posts, but can't quite get it right. The code below echos all of the files instead of just those that were created today. Any suggestions? Thanks.

$di = new RecursiveDirectoryIterator('/documents/listings');
foreach (new RecursiveIteratorIterator($di) as $filename => $file) 
{

    if (date ("m-d-Y", filemtime($file)) == date("m-d-Y")) 

    { echo $filename . '<br/>'; }

}

Upvotes: 3

Views: 1852

Answers (3)

cn0047
cn0047

Reputation: 17061

First of all, about function filemtime, it returns: file modification time.
But also exists function filectime, it returns: inode change time of file.
As i found filectime more better for your question, but it really returns files not only created today but also edited today.

My code:

<?php

$path = dirname(__FILE__);

$objects = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator($path),
    RecursiveIteratorIterator::SELF_FIRST
);
foreach ($objects as $file => $object) {
    $basename = basename($file);
    if ($basename == '.' or $basename == '..') {
        continue;
    }
    if (is_dir($file)) {
        continue;
    }
    if (date('Y-m-d') === date('Y-m-d', filectime($file))) {
        echo $file . PHP_EOL;
    }
}

Upvotes: 4

testo
testo

Reputation: 1260

if (!is_dir($file) && date ("m-d-Y", filemtime($file)) == date("m-d-Y")) 

Maybe you only want to display the files without the directories? Try this for condition.

Upvotes: -1

Manoj Dhiman
Manoj Dhiman

Reputation: 5166

your code is working for me . please check the file create date once.or try with change of directory you created in past

Upvotes: 1

Related Questions