PHP: How to display in a table, information from a text file

root's picture

Php is able to read files and pass that information into a website. There are, of course, many ways to do this and below is just a tiny example.

Let's say you have in a text file, several lines in this format:

user@gzlinux $ cat files/servers-status.txt

And you want to present this information in a php page with these tables: Hostname, Application, Number of Instances, Status.

Here is what your php page will need:

user@gzlinux $ cat envs.php
<title>GZ Media Environments</title>

<table class="table">
      <th>Number of instances</th>

$text = file_get_contents('files/servers-status.txt');
$lines = explode("\n",$text);

foreach ($lines as &$line) {
	list($hostname,$application,$no_instances,$status) = explode("#",$line);
	echo "<td class='small'>$hostname</td>";
	echo "<td class='small'>$application</td>";
	echo "<td class='small'>$no_instances</td>";
	echo "<td class='small'>$status</td>";


This solution is untested so do not use it in production until you are very satisfied with it.

Thou shalt not steal!

If you want to use this information on your own website, please remember: by doing copy/paste entirely it is always stealing and you should be ashamed of yourself!
Have at least the decency to create your own text and comments and run the commands on your own servers and provide your output, not what I did!

Or at least link back to this website.