2

I want to get all file names (and path) of files those updated after a date (in directory and subdirectories) using PHP.

like all files updated after 20.08.2017 ,

Below code provide only files from directory, i also need path,

$dir = "opendir(".")";
clearstatcache();
$yesdate = strtotime("-1 days");
while(false != ($file = readdir($dir)))
{
    if ( substr($file,-4) == ".php" ) 
    {
    if (filemtime($file) >= $yesdate)
    {
        echo $file;
    }
}

}

Thanks

infugin
  • 27
  • 6
  • 1
    Possible duplicate of [php get full path of file in a folder](https://stackoverflow.com/questions/29555958/php-get-full-path-of-file-in-a-folder) – jcjr Aug 20 '18 at 07:59
  • @jcjr , this question is with date – infugin Aug 20 '18 at 09:41

4 Answers4

1

If you're using relative paths like e.g. . or paths that follow a symbolic link, you can get the real path via the function realpath :

$actualDirectory = realpath(".");
$dir = opendir($actualDirectory);
clearstatcache();
$yesdate = strtotime("-1 days");
while(false != ($file = readdir($dir)))
{
    if ( substr($file,-4) == ".php" && filemtime($file) >= $yesdate)
    {
        echo $actualDirectory."/".$file;
    }
}
apokryfos
  • 38,771
  • 9
  • 70
  • 114
0

With a specified date:

$dir = opendir(dirname(__FILE__));
clearstatcache();
$dday = "20.08.2017.";
$yesdate = strtotime($dday);
while(false != ($file = readdir($dir))) {
   if ( substr($file,-4) == ".php" ) {
      if (filemtime($file) >= $yesdate) {
         echo $file;
      }
   }
}

Or with today - 1 day:

$dday = date("d.m.Y", time());
$yesdate = strtotime($dday) - 86400;
Gabor
  • 566
  • 5
  • 14
0

If you want to scan all sub directories until the end of the tree you need to use a recursive function.

function get_updated_files($date, $directory, $result = array())
{
    $directory = realpath($directory);
    $directory_content = glob($directory.'/*');
    foreach($directory_content as $item) {
    if(is_dir($item)) {
        $result = get_updated_files($date, $item, $result);
    } elseif(strtotime($date) < filemtime($item) && pathinfo($item, PATHINFO_EXTENSION) == 'php') {
        $result[] = $item;
    }
    }
    return $result;
}

$result = get_updated_files('2017-08-20', '.');
Samuil Banti
  • 1,735
  • 1
  • 15
  • 26
  • 1
    `$directory_content = glob($directory.'/*.php');` – Gabor Aug 20 '18 at 08:40
  • @Gabor if you set *.php you will not be able to can the sub directories. – Samuil Banti Aug 20 '18 at 08:53
  • 1
    @infugin one of the possible solutions is to use pathinfo to get the file extension. I've edited the script in the elseif clause. – Samuil Banti Aug 20 '18 at 08:54
  • @SamuilBanti , glob() function making memory exhausted error, 268435456 bytes exhausted (tried to allocate 32 bytes) – infugin Aug 22 '18 at 04:27
  • @SamuilBanti, what i am trying to do is, i am two level inside of root folder and i want to get files from root, if I go with realpath its only provide files inside that folder of file but i need files from root "$directory = "/home/dev/public_html";" and i am here "public_html/main/help/checkfile.php' – infugin Aug 22 '18 at 04:56
  • 1
    @infugin You need two things. 1. Set the second parameter of the function to the path that you need: get_updated_files('2017-08-20', '/home/dev/public_html'); 2. Make sure that the user that executes the script has the required permissions to read from the directory. – Samuil Banti Aug 23 '18 at 07:45
0

You must proceed like this

function get_updated_files($date, $directory,$file_extension='php',$sameday=false, $result = array())
{
    if(!$sameday){
        /* really worth because actually 
           your code will  return true for your given day and this maybe
           is not the goal you are trying to achieve...*/
        $date = strtotime($date)+86399;
    }else{
        $date=strtotime($date);
    }
    $directory = realpath($directory);
    $directory_content = glob($directory.'\\*');
    foreach($directory_content as $item) {
        if(is_dir($item)) {
            $result=get_updated_files($date, $item,$file_extension,$sameday,$result);
        } elseif($date < filemtime($item)&& pathinfo($item, PATHINFO_EXTENSION)===$file_extension) {
            $result[] = $item;
        }
    }




   return $result;
}

$result = get_updated_files('2017-08-20', '.');
Elementary
  • 1,443
  • 1
  • 7
  • 17
  • @apokrytos i made this post really generic.You can get all needed extensions when needed but the worth thing is that it takes care to not return true when the file has the same day as your given date... – Elementary Aug 20 '18 at 09:16