Deprecated: The each() function is deprecated. This message will be suppressed on further calls in /home/zhenxiangba/zhenxiangba.com/public_html/phproxy-improved-master/index.php on line 456
PHP: fam - Manual
[go: Go Back, main page]

PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

fam_cancel_monitor" width="11" height="7"/> <expect_popen
Last updated: Sun, 25 Nov 2007

view this page in

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

導入

FAM はファイルやディレクトリを監視し、変更点を調査を行うアプリケーションに 通知します。FAM についての詳細な情報は » http://oss.sgi.com/projects/fam/ で得られます。

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

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

注意: この拡張モジュールは » PECL レポジトリに移動 されており、以下のバージョン以降 PHP にバンドルされなくなっています。 PHP 5.1.0.

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

要件

この拡張モジュールは、SGI が開発した » FAM ライブラリの関数を使用しています。そのため、FAM ライブラリを ダウンロードしてインストールする必要があります。

インストール手順

PHP の FAM サポートを使用するには、--with-fam[=DIR] を指定して PHP をコンパイルする 必要があります。DIR は lib および include ディレクトリを含む ディレクトリの場所です。

実行時設定

設定ディレクティブは定義されていません。

リソース型

FAM モジュールでは二種類のリソース型を使用します。まず最初が FAM サービスとの接続を表すリソースで、これは fam_open() が返します。二番目はモニタリングリソースで、これは fam_monitor_XXX 関数が返します。

定義済み定数

以下の定数が定義されています。 この関数の拡張モジュールが PHP 組み込みでコンパイルされているか、 実行時に動的にロードされている場合のみ使用可能です。

FAM イベント定数
定数 説明
FAMChanged (integer) ファイルあるいはディレクトリの、fstat(1) で取得できる値のうちの 何かが変更されました。
FAMDeleted (integer) ファイルあるいはディレクトリが削除あるいはリネームされました。
FAMStartExecuting (integer) 実行可能ファイルが実行されました。
FAMStopExecuting (integer) 実行可能ファイルの実行が終了しました。
FAMCreated (integer) ディレクトリ内にファイルが作成されました。
FAMMoved (integer) このイベントは決して発生しません。
FAMAcknowledge (integer) fam_cancel_monitor() に対する応答イベントです。
FAMExists (integer) ファイルやディレクトリの監視を要求するイベントです。 ディレクトリが監視されている場合、ディレクトリおよびその中に 含まれるすべてのファイルについてのイベントが発生します。
FAMEndExist (integer) 最後の FAMEExists イベントの後に発生します。

目次



fam_cancel_monitor" width="11" height="7"/> <expect_popen
Last updated: Sun, 25 Nov 2007
 
add a note add a note User Contributed Notes
fam
Yassin Ezbakhe <yassin88 at gmail dot com>
01-Sep-2005 08:16
I make a VERY simple class that monitors a folder (and its subfolders) for new or removed files. I use it in order to auto index a folder where I have all my eBooks into a MySQL database.

CLASS FILE:

<?php
class FileAlterationMonitor
{
   
private $scanFolder, $initialFoundFiles;

   
public function __construct($scanFolder)
    {
       
$this->scanFolder = $scanFolder;
       
$this->updateMonitor();
    }

   
private function _arrayValuesRecursive($array)
    {
       
$arrayValues = array();

        foreach (
$array as $value)
        {
            if (
is_scalar($value) OR is_resource($value))
            {
                
$arrayValues[] = $value;
            }
            elseif (
is_array($value))
            {
                
$arrayValues = array_merge( $arrayValues, $this->_arrayValuesRecursive($value));
            }
        }

        return
$arrayValues;
    }

   
private function _scanDirRecursive($directory)
    {
       
$folderContents = array();
       
$directory = realpath($directory).DIRECTORY_SEPARATOR;

        foreach (
scandir($directory) as $folderItem)
        {
            if (
$folderItem != "." AND $folderItem != "..")
            {
                if (
is_dir($directory.$folderItem.DIRECTORY_SEPARATOR))
                {
                   
$folderContents[$folderItem] = $this->_scanDirRecursive( $directory.$folderItem."\\");
                }
                else
                {
                   
$folderContents[] = $folderItem;
                }
            }
        }

        return
$folderContents;
    }

   
public function getNewFiles()
    {
       
$finalFoundFiles = $this->_arrayValuesRecursive( $this->_scanDirRecursive($this->scanFolder));

        if (
$this->initialFoundFiles != $finalFoundFiles)
        {
           
$newFiles = array_diff($finalFoundFiles, $this->initialFoundFiles);
            return empty(
$newFiles) ? FALSE : $newFiles;
        }
    }

   
public function getRemovedFiles()
    {
       
$finalFoundFiles = $this->_arrayValuesRecursive( $this->_scanDirRecursive($this->scanFolder));

        if (
$this->initialFoundFiles != $finalFoundFiles)
        {
           
$removedFiles = array_diff( $this->initialFoundFiles, $finalFoundFiles);
            return empty(
$removedFiles) ? FALSE : $removedFiles;
        }
    }

   
public function updateMonitor()
    {
       
$this->initialFoundFiles = $this->_arrayValuesRecursive($this->_scanDirRecursive( $this->scanFolder));
    }
}
?>

A simple script that uses this class could be like this one (use it with PHP CLI):

<?php

$f
= new FileAlterationMonitor($MY_FOLDER_TO_MONITOR)

while (
TRUE)
{
   
sleep(1);

    if (
$newFiles = $f->getNewFiles())
    {
       
// Code to handle new files
        // $newFiles is an array that contains added files
   
}

    if (
$removedFiles = $f->getRemovedFiles())
    {
       
// Code to handle removed files
        // $newFiles is an array that contains removed files
   
}

   
$f->updateMonitor();
}
ca50015 at yahoo dot com
28-Nov-2004 02: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
12-Aug-2004 02: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
22-May-2004 01: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

fam_cancel_monitor" width="11" height="7"/> <expect_popen
Last updated: Sun, 25 Nov 2007
 
 
show source | credits | sitemap | contact | advertising | mirror sites