开发者

Watching a directory and redirecting to PHP

开发者 https://www.devze.com 2023-02-10 10:31 出处:网络
Greetings, I\'m watching a postfix directory using iwatch. iwatch /home/vmail/eamorr/photos/new/ When a new email is sent, iwatch outputs a line such as:

Greetings,

I'm watching a postfix directory using iwatch.

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

When a new email is sent, iwatch outputs a line such as:

[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?

Here's what I've tried:

iwatch /home/vmail/eamorr/photos/开发者_如何学Gonew/ | php /home/hynese/sites/eamorr/emailPhotoHandler/handlePhotos.php

And here's the handlePhotos.php file:

<?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...

I think it's something to do with the piping and stdin in PHP.

Anyone got any ideas?

Many thanks in advance,


file_get_contents() does not return until the entire file/stream has been read. 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.

Try stream_get_line() or fgets() instead. That will give you the iwatch output line-by-line.

Edit: also, the command should be php -f <script>, not php <script>.

0

精彩评论

暂无评论...
验证码 换一张
取 消