【问题标题】:Watching a directory and redirecting to PHP查看目录并重定向到 PHP
【发布时间】:2011-02-11 12:47:30
【问题描述】:

您好,

我正在使用 iwatch 观看一个后缀目录。

iwatch /home/vmail/eamorr/photos/new/

发送新邮件时,iwatch 会输出如下一行:

[11/Feb/2011 12:23:43] IN_CREATE /home/vmail/eamorr/photos/new//1297427022.Vca01I1e396M000383.eamorr

如何将其重定向到 PHP 程序进行处理?

这是我尝试过的:

iwatch /home/vmail/eamorr/photos/new/ | php /home/hynese/sites/eamorr/emailPhotoHandler/handlePhotos.php

这里是 handlePhotos.php 文件:

<?php
$data = file_get_contents("php://stdin");

$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $data;
fwrite($fh, $stringData);
fclose($fh);

?>

它应该创建一个文件“testFile.txt”并将“[11/Feb/2011 12:23:43] IN_CREATE /home/vmail/eamorr/photos/new//1297427022.Vca01I1e396M000383.eamorr”放入其中。 ..但我得到的只是什么-文件甚至没有创建...

我认为这与 PHP 中的管道和标准输入有关。

有人有什么想法吗?

非常感谢,

【问题讨论】:

  • iwatch 输出未流式传输到管道。
  • iWatch 接受-c 开关以对事件执行命令。试过了吗?
  • 好了,我可以执行命令了,但是如何获取被修改的文件名呢?
  • print_r($_SESSION) 并且其中一个变量将包含您要查找的内容。
  • 我已经尝试过以下命令:iwatch -v /home/vmail/eamorr/photos/new/ -c "echo '%f' &gt; hi.txt ",但从未创建 hi.txt!

标签: php file stdin pipe watch


【解决方案1】:

file_get_contents() 在读取整个文件/流之前不会返回。在这种情况下,永远不会。假设 iwatch 继续运行,iwatch 将保持 PHP 的 STDIN 打开,因此 file_get_contents() 永远不会到达终点。

改用stream_get_line()fgets()。这将为您逐行提供 iwatch 输出。

编辑:另外,命令应该是php -f &lt;script&gt;,而不是php &lt;script&gt;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-05
    • 1970-01-01
    • 1970-01-01
    • 2012-10-30
    • 1970-01-01
    • 2017-08-04
    • 1970-01-01
    • 2014-01-17
    相关资源
    最近更新 更多