How to get PHP files and their path changed after a date

php get last modified file in directory
filemtime(): stat failed for
php filectime
last modified date and time in php
php date created file
php filemtime not working
php last-modified header
php get file size

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

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', '.');

How to get PHP files and their path changed after a date, Get the last time the file content was modified: <?php echo "<br>"; echo "​Content last changed: ".date("F d Y H:i:s. Specifies the path to the file to check  PHP Date and Time PHP Include PHP File Handling PHP File Open/Read PHP File Create/Write PHP File Upload PHP Cookies PHP Sessions PHP Filters PHP Filters Advanced PHP JSON PHP OOP PHP What is OOP PHP Classes/Objects PHP Constructor PHP Destructor PHP Access Modifiers PHP Inheritance PHP Constants PHP Abstract Classes PHP Traits PHP Static

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;
    }
}

PHP filemtime() Function, PHP Exercises, Practice and Solution: Write a PHP script to get last modified information of a file. PHP : Exercise-15 with Solution. Write a PHP script Note​: The result may vary for your system date and time. basename() function: The basename(path,suffix) function is used to get the filename from a path. To get the exact path of file, then, you can use realpath(). In between the round brackets of the function, type the name of the file. In between the round brackets of the function, type the name of the file.

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;

PHP Exercise: Get last modified information of a file, How to make a spider-bot in PHP ? The filemtime() function returns the last time the file was changed as a of a file when its content was modified as a Unix Timestamp on success and False on failure. Input : echo filemtime("gfg.txt"); Output : 1525159574 Input : echo "Last modified: ".date("F d Y H:i:s. However, in the phpscript, I can only get the filename and not the complete path of the file specificed. fopen() fails due to this reason. Can anyone please let me know how I can work on finding the complete path?

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', '.');

PHP, use Symfony\Component\Finder\Finder; $finder = new Finder(); // find all files in the current As the Finder uses PHP iterators, you can pass any URL with a supported PHP wrapper for matches files that contain "data" anywhere in their paths (files or directories) Find files by last modified dates with the date() method:. To get the modification date of some remote file, you can use the fine function by notepad at codewalker dot com (with improvements by dma05 at web dot de and madsen at lillesvin dot net). But you can achieve the same result more easily now with stream_get_meta_data (PHP>4.3.0). However a problem may arise if some redirection occurs.

The Finder Component (Symfony Docs), There are two ways to list files in given directory modified after given date of the current year. The latest version of find /path/to/dir -newermt "yyyy-mm-dd" -print -type f -iname "*.pl" Sample outputs: Fig.01: images/advanced-cache.php . As I said above on Jonathan's answer, this will grab the folders which have changes causing all of their files to be imported. Add -type f to only select files. This maintains the folder structure, but only backs up the files with changes/added recently. – Brent Aug 26 '13 at 16:20

Linux / Unix: Find Files Modified On Specific Date, For more general purposes, you might want to get the file size so that you can This function requires the file path and returns the date that the file was last when a file's contents are modified or when changes are made to its permissions or  If the path has more than one extension, PATHINFO_EXTENSION returns only the last one and PATHINFO_FILENAME only strips the last one. (see first example below). Note: If the path does not have an extension, no extension element will be returned (see second example below).

Sams Teach Yourself PHP, MySQL and Apache All in One: STY PHP, , Besides their contents, files have other properties that can provide useful information. A file is considered changed if it is created or written, or when its permissions UNIX epoch (midnight on January 1, 1970) and the specified time and date. It's often very useful to be able to separate a filename from its directory path,  Rewriting Semantic URLs with htaccess for improved SEO and improved user experience. Hide your PHP file extension in URL. http://www.ruvictor.com/article/cha

Comments
  • Possible duplicate of php get full path of file in a folder
  • @jcjr , this question is with date
  • $directory_content = glob($directory.'/*.php');
  • @Gabor if you set *.php you will not be able to can the sub directories.
  • @infugin one of the possible solutions is to use pathinfo to get the file extension. I've edited the script in the elseif clause.
  • @SamuilBanti , glob() function making memory exhausted error, 268435456 bytes exhausted (tried to allocate 32 bytes)
  • @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.
  • this is not provinding files path and how to get root file if this file is inside 2 folder ?
  • @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...