Reputation: 35
I want to tail a dated log file LIVE on the administrator page of my website, but I'm running in to problems. I can get it to work only if I manually input the date in the $filename variable.
Here is the code I found on here too, but would like to know how to get dated files (ex: 2016_04_26.ilog).
<?php
$filename = 'date('Y_m_d').ilog';
$filedir = '/gamelogs/';
$output = shell_exec('exec tail -n250 ' . $filedir$filename');
echo str_replace(PHP_EOL, '<br />', $output);
?>
What is wrong with my code? Thanks in advance!
So after taking a break and coming back to the code, I realized I had the date format wrong and a couple other parts. Here is what I have that works!
<?php
$filename = date('y_m_d').'.ilog'; // misplaced quotes
$filedir = '/home/a3invmgr/destinedtobe/a3ilogs/'; // relative path instead of full path
$output = shell_exec("tail -n100 $filedir$filename"); //exec removed
echo str_replace(PHP_EOL, '<br />', $output); // line OK
?>
Upvotes: 2
Views: 3618
Reputation: 98921
You've a couple of errors, try this:
<?php
$filename = date('Y_m_d').'.ilog'; // misplaced quotes
$filedir = '/full/path/to/gamelogs/'; // relative path instead of full path
$output = shell_exec("tail -n250 {$filedir}{$filename}"); //exec removed
echo str_replace(PHP_EOL, '<br />', $output); // line OK
?>
Upvotes: 1
Reputation: 631
I think your initial $filename
might be wrong - I see too many quotes. Assuming that filenames look like 2016_04_27.ilog
then you should use:
$filename = date('Y_m_d'). '.ilog';
Also, to further troubleshoot, you might try putting the execution command into a variable and outputting that:
$stringToExecute = ('tail -n250 ' . $filedir$filename);
echo $stringToExecute;
$output = shell_exec(stringToExecute);
Lastly, if you're echoing log files, you might just want to skip the str_replace
and just wrap the results in a <pre>
. This will tell the browser to keep newlines intact.
echo "<pre>$output</pre>";
Upvotes: 0
Reputation: 7113
Two problems that need fixing:
$filedir$filename
that is not a valid php variable.
You have an apostrophe attacking your code here
$filedir$filename');
Take the apostrophe out.
Fix those two issues and let me know if your problem is solved.
Upvotes: 0