<html>
<head>
<title>Retrieve data from file</title>
<style>
    A:link    {color:red;   text-decoration:none}
    A:visited {color:teal;  text-decoration:none}
    A:hover   {color:white; text-decoration:none; background-color:red}

    tr:nth-child(odd)  {background-color:white}
    tr:nth-child(even) {background-color:cccccc}
</style>
</head>
<body bgcolor=lightyellow>
<h1><center>Read a File from the Server</center></h1>
<h2>Read file with paging </h2>

<?php include "../../include.php"; ?>                  <!-- hyperlink to see the code -->
<?php
    error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
?>
    
<form  method=get action=<?php echo $_SERVER['PHP_SELF']?> >
Filename <input type=text name=file size=30 value="<?php echo $_GET['file']?>" placeholder='Use absolute path'> (e.g. /var/log/httpd/error_log)
</form>

<?php
    error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);

    $allow_dir = false;                             #allow directory read?                                                      

    $filename = $_GET['file'];                      #server file to be read
    $page     = $_GET['page'];                      #starting page num to read 
    $limit    = $_GET['limit'];                     #number of lines to read

    if (!$page)  $page  = 1;
    if (!$limit) $limit = 1000;                     #Page size = 1000 lines

    if ($filename)
    {
        read_data();
        display();
    }

//===============================================================================
//  Read a file or a directory
//  If a file, limit the number of records to 1000 (default)
//===============================================================================
    function read_data()
    {
        global $filename, $page, $limit, $total_pages, $output, $allow_dir;   
 
        $line_num   = 0;                                        //line number within the file
        $line_count = 0;                                        //line count in the output array
        $line_start = ($page-1) * $limit;                       //line number to start readling from

        if (is_dir($filename) && !$allow_dir)                   //if directory listing not allowed
            die ("Diectory listing not allowed");               //exit 
                
        if (is_dir($filename))                                  //if the file is a directory
        {
            $handle = opendir ($filename)  
                      or die ("Cannot open $filename as a directory"); 
                
            while ($line = readdir($handle)) 
                if ($line != '.' && $line != '..')              //if not current or parent directory 
                    $output[] = "$line \n";                     //add to the output array
                            
            closedir($handle);           
            sort($output);
        }                 
        else                                                    //if the file is a regular file
        { 
            $handle = fopen ($filename, 'r')  
                      or die ("Cannot open $filename - File does not exist, or permission denied"); 

            while($line = fgets($handle))
            {
                $line_num++;                

                if ($line_num >= $line_start && $line_count < $limit)
                    $output[$line_count++] = $line;                 //add to the output array
            }
            fclose($handle);
            
            $total_pages = ceil($line_num / $limit);                //total number of pages 
        }
    }

//===============================================================================
//  Display a file content or a directory listing
//  If a file, provide paging capabilities.  default each page = 1000 records
//===============================================================================
    function display()
    {
        global $filename, $page, $limit, $total_pages, $output;   
        
        $script = $_SERVER['PHP_SELF']; 

        if (is_dir($filename))
        {
            foreach($output as $rec)
                print "<li> <a href=$script?file=$filename/$rec> $rec </a> <br>\n ";
        }
        else    
        {
            $prev_page = max($page-1, 1);                       //not less than 1
            $next_page = min($page+1, $total_pages);            //not more than total pages
            
            $num = ($page-1) * $limit;                          //starting line number 
                                                      
            print "Page \n";
            print "<a href=$script?file=$filename&page=$next_page>>></a> \n";

            for($page=1; $page<=$total_pages; $page++)            //create the links for multiple pages
                print "<a href=$script?file=$filename&page=$page>$page</a> \n";

            print "<a href=$script?file=$filename&page=$prev_page><<</a> \n";

            print "<table style='border:solid black 1px;background-color:white'> \n";
            foreach($output as $rec)
            {
                $num++;
                print "<tr><td><b><i>$num <td><nobr>  $rec ";
            }        
            print "</table> \n";
        }
    }
//===============================================================================
?>