简体   繁体   English

在windows上观看的PHP文件夹

[英]PHP folder watching on windows

I am writing a simple PHP script to watch a folder and its subfolders for any changes (new files, modifications, deletions) and then perform an action. 我正在编写一个简单的PHP脚本来查看文件夹及其子文件夹以进行任何更改(新文件,修改,删除),然后执行操作。

I will run this script from the commandline on windows using php -f script.php . 我将使用php -f script.php从Windows上的命令行运行此脚本。

I have been search for a way to watch folders on windows that has PHP bindings. 我一直在寻找一种方法来观看具有PHP绑定的Windows上的文件夹。 Something like inotify or gamin for windows would be nice. 像inotify或gamin for windows会很不错。

The answers to this question mentions FindFirstChangeNotification , but I couldn't find any PHP bindings for it. 这个问题的答案提到了FindFirstChangeNotification ,但我找不到任何PHP绑定。

Are there any libraries/software for folder/filesystem watching on windows with PHP bindings? 在使用PHP绑定的窗口上是否有用于文件夹/文件系统的库/软件?

I ended up just writing a simple function using the RecursiveDirectoryIterator that is called in an infinit loop. 我最后只是使用在infinit循环中调用的RecursiveDirectoryIterator编写一个简单的函数。

All I have to do is check the last modified time of the file or a folder and return true or false. 我所要做的就是检查文件或文件夹的上次修改时间并返回true或false。

This isn't a very exact approach, but it serves my purposes well. 这不是一个非常精确的方法,但它很好地满足了我的目的。 Sitting in the back ground, the script uses about 12MB of ram. 脚本坐在后面,使用大约12MB的内存。

You can use inotify_add_watch PHP function to get notification of any changes (new files, modifications, deletions) in specified directory. 您可以使用inotify_add_watch PHP函数来获取指定目录中的任何更改(新文件,修改,删除)的通知。 It works the same way as FileSystemWatcher on Windows. 它的工作方式与Windows上的FileSystemWatcher相同。

If you have Ruby installed on your server you can use watchr gem 如果您在服务器上安装了Ruby,则可以使用watchr gem

What it does is very simple, upon fire/directory change it executes a script defined by you which is exactly what you're trying to do. 它的作用非常简单,在更改/目录更改时,它会执行您定义的脚本,这正是您尝试执行的操作。

Here is an example file autotest.rb : 这是一个示例文件autotest.rb

#!/usr/bin/ruby

# Match all PHP files in your project directory
watch("<PROJECT_DIR_PATH>/(.*).<FILE_EXTENSION_PHP>") do |match|
  run_test %{<PROJECT_DIR_PATH>/Tests/#{match[1]}Test.php}
end

# Match all files in your Tests directory
watch("<PROJECT_DIR_PATH>/Tests/.*Test.php") do |match|
  run_test match[0]
end

# Run test if there are matches
def run_test(file)
  unless File.exist?(file)
    puts "#{file} does not exist"
    return
  end

  puts "Running #{file}"
  result = `phpunit #{file}`
  puts result
end

So this wil match all PHP or any other extension files and run RegEx against the name of the file and if there is a match like /Project/Tests/ClassNameTest.php it will run the test otherwise just terminate with massage. 所以这将匹配所有PHP或任何其他扩展文件并对文件的名称运行RegEx,如果有像/Project/Tests/ClassNameTest.php这样的匹配,它将运行测试,否则只需按下终止。 For convenience this could be set to send emails upon errors to predefined list emails. 为方便起见,可以设置为在发生错误时将电子邮件发送到预定义的列表电子邮件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM