Using Perl File::Tail in php code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Artex7
Forum Newbie
Posts: 6
Joined: Fri Nov 14, 2008 9:18 pm

Using Perl File::Tail in php code

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Using Perl File::Tail in php code

Post 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.
Artex7
Forum Newbie
Posts: 6
Joined: Fri Nov 14, 2008 9:18 pm

Re: Using Perl File::Tail in php code

Post 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?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Using Perl File::Tail in php code

Post 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.
Artex7
Forum Newbie
Posts: 6
Joined: Fri Nov 14, 2008 9:18 pm

Re: Using Perl File::Tail in php code

Post 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.
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Re: Using Perl File::Tail in php code

Post by Syntac »

Code: Select all

$contents = file("path/to/log.txt");
echo array_pop($contents);
This should work.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Using Perl File::Tail in php code

Post by Eran »

Code: Select all

$handle = popen("tac " . $filename,'r');
$line = fgets($handle);
Will read and return only the last line.
Artex7
Forum Newbie
Posts: 6
Joined: Fri Nov 14, 2008 9:18 pm

Re: Using Perl File::Tail in php code

Post 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.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Using Perl File::Tail in php code

Post 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.
Artex7
Forum Newbie
Posts: 6
Joined: Fri Nov 14, 2008 9:18 pm

Re: Using Perl File::Tail in php code

Post 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)
"
Last edited by Artex7 on Sun Nov 16, 2008 10:58 pm, edited 1 time in total.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Using Perl File::Tail in php code

Post 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.
Artex7
Forum Newbie
Posts: 6
Joined: Fri Nov 14, 2008 9:18 pm

Re: Using Perl File::Tail in php code

Post 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?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Using Perl File::Tail in php code

Post 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.
Post Reply