简体   繁体   English

查看目录并重定向到PHP

[英]Watching a directory and redirecting to PHP

Greetings, 问候,

I'm watching a postfix directory using iwatch. 我正在使用iwatch查看postfix目录。

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

When a new email is sent, iwatch outputs a line such as: 发送新电子邮件时,iwatch输出以下行:

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

How do I redirect this to a PHP program for processing? 如何将其重定向到PHP程序进行处理?

Here's what I've tried: 这是我尝试过的:

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

And here's the handlePhotos.php file: 这是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);

?>

It should create a file "testFile.txt" and put "[11/Feb/2011 12:23:43] IN_CREATE /home/vmail/eamorr/photos/new//1297427022.Vca01I1e396M000383.eamorr" into it... But all I'm getting is nothing - the file isn't even created... 它应该创建一个文件“ testFile.txt”,并将“ [11 / Feb / 2011 12:23:43] IN_CREATE /home/vmail/eamorr/photos/new//1297427022.Vca01I1e396M000383.eamorr放入其中...但是我得到的只是什么-文件甚至都没有创建...

I think it's something to do with the piping and stdin in PHP. 我认为这与PHP中的管道和标准输入有关。

Anyone got any ideas? 任何人有任何想法吗?

Many thanks in advance, 提前谢谢了,

file_get_contents() does not return until the entire file/stream has been read. 在读取了整个文件/流之前,file_get_contents()不会返回。 In this case, that's never. 在这种情况下,永远不会如此。 Assuming that iwatch keeps running, iwatch will keep PHP's STDIN open so file_get_contents() never gets to the end. 假设iwatch继续运行,iwatch将使PHP的STDIN保持打开状态,因此file_get_contents()永远不会结束。

Try stream_get_line() or fgets() instead. 请尝试使用stream_get_line()fgets() That will give you the iwatch output line-by-line. 这将为您提供iwatch逐行输出。

Edit : also, the command should be php -f <script> , not php <script> . 编辑 :同样,命令应该是php -f <script> ,而不是php <script>

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

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