How to check if a file has changed?

后端 未结 5 669
梦毁少年i
梦毁少年i 2020-12-20 02:53

I have an xml file with database information that should be loaded when the script is installed or when the content of the file changes. Can I use md5_file() on the xml file

相关标签:
5条回答
  • 2020-12-20 03:23

    Your best bet would be to store the file mod time and compare it to a file mod time you check in the future.

    if(filemtime('myfile.txt') > $result_set['filemtime']) {
        // file was modified
    }
    

    I have hope that you can do the databasing yourself.

    0 讨论(0)
  • 2020-12-20 03:23

    I know that I am going back in time here, but the question helped me so I want to share what I did with it, so while this is not an answer to the question I will make it a community wiki so it can be improved (I'm no expert).

    This was part of a much larger class so I have stripped out the rest and provided just the applicable code and turned it into an easy to follow example using a very common file:

    class ApplicationLogger {
    
        private $logfile, $path;
    
        public function __construct() {
            $this->logfile  = 'error.log';
            $this->path  = '/var/log/apache2/';
        }
    
        public function logState() {
            $files = array(
                'target'  => $this->path . $this->logfile,
                'lastmod' => $this->path . $this->logfile . '_lastmod'
            );
    
            if (file_exists($files['lastmod']) && file_exists($files['target'])) {
                $lfh = fopen($files['lastmod'], 'r');
                while (!feof($lfh)) {
                    $lines[] = fgets($lfh);
                }
                fclose($lfh);
            }
    
            $modified = false;
    
            /**
             * check if we have a matching hash.
             */
            if (isset($lines) && filemtime($files['target']) != $lines[0]) { // mod time mismatch
                if (md5_file($files['target']) != $lines[1]) { // content modified
                    $modified = true;
                }
            }
    
            /**
             * update or create the lastmod file
             */
            if (!file_exists($files['lastmod']) || $modified) {
    
                $current_mod = filemtime($files['target']) . "\n" . md5_file($files['target']);
    
                file_put_contents($files['lastmod'], $current_mod);
                $modified = true;
            }
    
            return $modified;
        }
    }
    

    Usage is straight forward:

    $mod = new ApplicationLogger();
    if ($mod->logState()) {
        // changed do something
    }
    

    Adapt it to your needs, improve it however you see fit. I hope that you will contribute your adaptations by editing this CW.

    0 讨论(0)
  • 2020-12-20 03:28

    Perhaps you could use PHP's filemtime() function. Simply put, this function gets file modification time for a specified file.

    This function returns a unix time stamp, so you'll need to save the last known modification time somewhere in order to compare it to the new value.

    $modifiedTs = filemtime($filename);
    if ($modifiedTs != $lastModificationTs){
      echo "$filename was modified!";
    }
    
    0 讨论(0)
  • 2020-12-20 03:31

    The simpliest way is to compare the file_date - you can use filemtime for this

    0 讨论(0)
  • 2020-12-20 03:42

    It depends on what you mean by "changed". If you require that the contents actually be modified, then checking filemtime is not enough - it's a fantastic first step, and should be used first, but it is not sufficient on its own.

    Conbine the filemtime with a hash of the file's contents (such as md5_file) and it will work efficiently.

    0 讨论(0)
提交回复
热议问题