Page 1 of 1

Using Perl File::Tail in php code

Posted: Fri Nov 14, 2008 9:30 pm
by Artex7
Hi all,

I'm a novice when it comes to working in both php and perl so I was wondering what would be the best way to go about this?
I'm trying to include Perl's File::Tail within some php code that I'm currently working on. The code looks as follows:

#!/usr/bin/perl -w
BEGIN { unshift @INC,"./blib/lib/";}
use File::Tail 0.8;

$name="/home/WorkSpace/logfile" unless $name=shift @ARGV;
$debug=shift @ARGV || 0;
print "Looking at $name \n";

$file=File::Tail->new(name=>$name,debug=>$debug,interval=>1,maxinterval=>5,
adjustafter=>10,resetafter=>500,errmode=>"return") or
die "Could not open $name: $!";
while ($line=$file->read)
{print "$line";}

It's a pretty dry cut/paste of one of the existing test files but I want to work with the $line thats returned in my php code.
So any thoughts on the best way to go about this? Thanks.

Re: Using Perl File::Tail in php code

Posted: Fri Nov 14, 2008 10:02 pm
by requinix
Are you talking about mixing PHP and Perl code together? I sure hope not.

You could make the PHP script run the Perl file using something like exec or system.

Re: Using Perl File::Tail in php code

Posted: Fri Nov 14, 2008 10:54 pm
by Artex7
I'm using File::Tail to monitor a log file so are you saying I could use exec or system to run the Perl file and return it's output back to within the php code?

Re: Using Perl File::Tail in php code

Posted: Fri Nov 14, 2008 11:19 pm
by requinix
Actually, I take what I said back. You'd best do all the coding in PHP.
The Perl code will never stop, so exec/system will never return.

Try a combination of fopen, fgets, and stream_set_blocking.

Code: Select all

$h = fopen("/home/WorkSpace/logfile", "r");
stream_set_blocking($h, true);
 
for (;true;) echo fgets($h);
Admittedly, not as flexible as the Perl you posted, but works well nonetheless.

Re: Using Perl File::Tail in php code

Posted: Sat Nov 15, 2008 6:25 pm
by Artex7
This works but the fgets outputs everything in the file and I would just need the last line for what I'm trying to do. Is there a way to have to ignore everything else and just send me back the last line of the log file?

And I know I haven't said it so thanks for the help.

Re: Using Perl File::Tail in php code

Posted: Sat Nov 15, 2008 6:31 pm
by Syntac

Code: Select all

$contents = file("path/to/log.txt");
echo array_pop($contents);
This should work.

Re: Using Perl File::Tail in php code

Posted: Sat Nov 15, 2008 7:33 pm
by Eran

Code: Select all

$handle = popen("tac " . $filename,'r');
$line = fgets($handle);
Will read and return only the last line.

Re: Using Perl File::Tail in php code

Posted: Sun Nov 16, 2008 9:53 pm
by Artex7
Argh, yet another hitch. I've combine the codes above to get this:

$filename = "/home/WorkSpace/logfile";
$h = popen("tac " . $filename, "r");
stream_set_blocking($h, true);
$line = fgets($h);
echo $line;

Now when I run this code in the terminal "php tail_file.php," I receive the last line in the log file. However, when I include this into my index.php for my Apache homepage, the last line is not echoed on the web-browser. How come and what can I do to fix it? Thanks again.

Re: Using Perl File::Tail in php code

Posted: Sun Nov 16, 2008 10:03 pm
by Eran
Dump everything that can go wrong:

Code: Select all

 
$filename = "/home/WorkSpace/logfile";
 
echo 'File is readable: ';
var_dump(is_readable($filename));
 
$h = popen("tac " . $filename, "r");
echo '<br />Handle is a pointer: ';
var_dump($h);
 
stream_set_blocking($h, true);
$line = fgets($h);
//echo $line;
echo '<br />Line is: ';
var_dump($line);
 
Of course this too verbose, but you get the picture. Post here the results of your dumps.

Re: Using Perl File::Tail in php code

Posted: Sun Nov 16, 2008 10:28 pm
by Artex7
File is readable: bool(false)
Handle is a pointer: resource(2) of type (stream)
Line is: bool(false)

However when I run it in the terminal, "php lastlog.php" I get the following:
File is readable: bool(true)
<br />Handle is a pointer: resource(4) of type (stream)
<br />Line is: string(63) "The line I want (text)
"

Re: Using Perl File::Tail in php code

Posted: Sun Nov 16, 2008 10:55 pm
by Eran
File is not readable. Is the path absolute? does it have the right permissions?
Remember that PHP running in the command line is running under the user you are logged in as, possibly root. In the browser PHP runs as apache, so the file should be accessible by apache as well.

Re: Using Perl File::Tail in php code

Posted: Sun Nov 16, 2008 11:16 pm
by Artex7
Troubleshooting it: I chmod 777 the file just to make sure it had all permissions but it still couldn't read it. Next I moved the file to under "/" and it was able to read it then. So I'm guessing there is something wrong with my path to the file? The path I have is actually "/home/<myusername>/WorkSpace/logfile" so does it have something to do with being under my username?

Re: Using Perl File::Tail in php code

Posted: Mon Nov 17, 2008 2:21 am
by Eran
Permissions are not enough, check the owner of the file / directory (run ls -la). It is probably root root, and you need to chown it to apache (chown = change owner) for apache to be able to read it.