PHP: ファイル改変監視関数(FAM) - Manual
PHP  
downloads | documentation | faq | getting help | mailing lists | | php.net sites | links | my php.net 
search for in the  
<read_exif_datafam_cancel_monitor>
view the version of this page
Last updated: Tue, 21 Dec 2004

XXXI. ファイル改変監視関数(FAM)

導入

FAMは、ファイル、ディレクトリを監視し、変更点を調査を行うアプリケーションに 通知します。

PHPスクリプトは、この拡張モジュールにより提供される関数を用いてFAMに一連の ファイルを指定することができます。

FAMプロセスは、最初にアプリケーションから接続された時に開始され、 全ての接続がクローズされた時に終了します。

注意: この拡張モジュールはWindows環境 では利用できません。

要件

この拡張モジュールは、... バージョン ... で入手可能な ...

実行用の設定

この拡張モジュールは設定ディレクティブを全く定義しません。

定義済みの定数

これらの定数は、この拡張モジュールで定義されており、 この拡張モジュールがPHP内部にコンパイルされているか実行時に動的にロー ドされるかのどちらかの場合のみ使用可能です。

表 1. FAM constants

Constantmeaning
FAMChanged (integer) The status of
FAMDeleted (integer)  
FAMStartExecuting (integer)  
FAMStopExecuting (integer)  
FAMCreated (integer)  
FAMMoved (integer)  
FAMAcknowledge (integer)  
FAMExists (integer)  
FAMEndExist (integer)  

目次
fam_cancel_monitor -- 監視を終了する
fam_close -- FAM接続を閉じる
fam_monitor_collection -- 指定したディレクトリにあるファイルの変更を監視する
fam_monitor_directory -- ディレクトリの変更を監視する
fam_monitor_file -- 通常のファイルの変更を監視する
fam_next_event -- 次のイベントを返す
fam_open -- FAMデーモンへの接続をオープンする
fam_pending -- 待機中のFAMイベントの有無を調べる
fam_resume_monitor -- 中断された監視処理を再開する
fam_suspend_monitor -- 監視を一時的に中断する


add a note add a note User Contributed Notes
ファイル改変監視関数(FAM)
ca50015 at yahoo dot com
27-Nov-2004 05:17
if u want do recursive monitoring on directory tree,
dont use fam_monitor_directory()
try to use fam_monitor_collection() instead
:)
b e n a d l e r -at- g m x -d-o-t- net
11-Aug-2004 05:57
in the example above, I would not use the while(1) and sleep() functions, since then you're back at waiting and polling for file changes, which you were trying to avoid using fam.

Instead, you can use while(fam_next_event($resource)), which blocks until there is an event. No polling, no useless wasting of cpu  cycles.

Anyway, I have some problems with fam: It is very limited and almost useless.

 - You cannot monitor a directory tree easily, since fam_monitor_directory() is non-recursive. Its an ugly hack, but you can go through each subdir and also monitor it with fam_monitor_directory().

 - When you do that, you will have another problem: The events you get contain the code and the filename, but not the pathname of the file that caused the event. Thus, if you monitor two or more directories, and they possibly contain files with the same basename, you cannot find out what file has changed. One ugly solution might be to create a new fam_resource for each directory you want to monitor, but then you cannot use fam_next_event() in your while loop anymore.

 - When a big file is saved (i.e. with photoshop via samba), you get file_changed events pretty much every second the file is being written to. It is not possible to receive ONE event AFTER the file operation is done.

I'm not sure, but I think most of this is not PHPs fault. Its just that fam (or dnotify, which fam uses on linux) sucks very badly. If you search on google, it seems everyone hates fam/dnotify (even linus), but noone has done anything about it yet.

If you find out how to work around thing, or if I'm completely wrong about all of this, please post here! Thanks!
sergiopaternoster at no_spam_tiscali dot it
21-May-2004 04:20
Here is a simple script to check changes etc. to a file.
<?php

/* opens a connection to the FAM service daemon */
$fam_res = fam_open ();

/*
 * The second argument is the full pathname
 * of the file to monitor.
 * Note that you can't use relative pathnames.
*/
$nres = fam_monitor_file ( $fam_res, '/home/sergio/test/fam/file_to_monitor.log');

while(
1){
   if(
fam_pending ( $fam_res ) ) $arr = (fam_next_event($fam_res)) ;
   switch (
$arr['code']){
       case
1:
           echo
"FAMChanged\n";
           break;
       case
2:
           echo
"FAMDeleted\n";
           break;
       case
3:
           echo
"FAMStartExecuting\n";
           break;
       case
4:
           echo
"FAMStopExecuting\n";
           break;
       case
5:
           echo
"FAMCreated\n";
           break;
       case
6:
           echo
"FAMMoved\n";
           break;
       case
7:
           echo
"FAMAcknowledge\n";
           break;
       case
8:
           echo
"FAMExists\n";
           break;
       case
9:
           echo
"FAMEndExist\n";
           break;
       default:
           break;
   }
   if(isset(
$arr)) unset($arr);
  
  
/* In order to avoid too much CPU load */
  
usleep(5000);
}

/* Close FAM connection */
fam_close($fam_res);

?>
Hope this could help.
God Belss PHP!
regards
Sergio Paternoster

<read_exif_datafam_cancel_monitor>
 Last updated: Tue, 21 Dec 2004
show source | credits | sitemap | contact | advertising | mirror sites 
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: /
Last updated: Mon Mar 14 08:13:06 2005 Local time zone must be set--see zic manual page