Add new comment

PHP - CSV Viewer for Web Browsers

By Daniel
Wed, 07/05/2017 - Updated 1 year ago
0 comments

PHP script that opens and parses a CSV table file to an HTML format to be displayed in the browser.

Description

This PHP script opens and parses a CSV table file to an HTML view. It can be used together with the Internet of Things Web Logger Logger script.

Features

  • Refresh, Download and Up/Bottom links.
  • Line numbers.
  • Header and footers with the column names.
  • Ready for CSS customization.
  • If a password is set, the GET parameter ?Password=yourpassword will be required to read the data. Example: http://www.mysite.com/csvview.php?Password=yourpassword.

Requirements

  • Webserver with PHP 5.1.x or higher installed.
  • Permissions for the script to read the CSV file.

Installation

Just edit the script configuration parameters (the CSV file name is the only required) and then put in the server and run.

Source code

  1. <?php
  2. /* =============================================================
  3. / * CSV Viewer
  4. * Version 1.0 (05/07/2017)
  5. *
  6. * This application loads and parses a CSV file in the HTML format for browser viewing.
  7. * Optionally the user can set a password in the configuration and then enter it using a GET request:
  8. * Example: www.mysite.com/csvlogview.php?Password=mypassword
  9. *
  10. * Developed by Daniel Brooke Peig - [email protected]
  11. * http://www.danbp.org
  12. * Copyright 2017 - Daniel Brooke Peig
  13. *
  14. * This software is distributed under the MIT License.
  15. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
  16. *
  17. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. *
  21. *
  22. /* =============================================================*/
  23.  
  24. //------------------------------------------------
  25. //Configuration
  26. //
  27. $fileName = "iotcsvlog.csv"; //CSV file location
  28. $delimiter = ";"; //CSV delimiter character: , ; /t
  29. $enclosure = '"'; //CSV enclosure character: " '
  30. $password = ''; //Optional to prevent abuse. If set to [your_password] will require the &Password=[your_password] GET parameter to open the file
  31. $ignorePreHeader = 3; //Number of characters to ignore before the table header. Windows UTF-8 BOM has 3 characters.
  32. //------------------------------------------------
  33.  
  34. //Variable initialization
  35. $logLines = array();
  36. $tableOutput = "<b>No data loaded</b>";
  37.  
  38. //Verify the password (if set)
  39. if($_GET["Password"] === $password || $password === ""){
  40.  
  41. if(file_exists($fileName)){ // File exists
  42.  
  43. // Reads lines of file to array
  44. $fileLines = file($fileName, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
  45.  
  46. //Not Empty file
  47. if($fileLines !== array()){
  48.  
  49. //Extract the existing header from the file
  50. $lineHeader = array_shift($fileLines);
  51. $logOriginalHeader = array_map('trim', str_getcsv(substr($lineHeader,$ignorePreHeader), $delimiter, $enclosure));
  52.  
  53. //Process the file only if the system could find a valid header
  54. if(count($logOriginalHeader) > 0) {
  55. //Open the table tag
  56. $tableOutput="<TABLE style='min-width: 50%;'>";
  57.  
  58. //Print the table header
  59. $tableOutput.="<TR style='background-color: lightgray;text-align:center;'>";
  60. $tableOutput.="<TD><B>Row</B></TD>";
  61. foreach ($logOriginalHeader as $field)
  62. $tableOutput.="<TD><B>".$field."</B></TD>"; //Add the columns
  63. $tableOutput.="</TR>";
  64.  
  65. //Get each line of the array and print the table files
  66. $countLines = 0;
  67. foreach ($fileLines as $line) {
  68. if(trim($line) !== ''){ //Remove blank lines
  69. $countLines++;
  70. $arrayFields = array_map('trim', str_getcsv($line, $delimiter, $enclosure)); //Convert line to array
  71. $tableOutput.="<TR><TD style='background-color: lightgray;'>".$countLines."</TD>";
  72. foreach ($arrayFields as $field)
  73. $tableOutput.="<TD>".$field."</TD>"; //Add the columns
  74. $tableOutput.="</TR>";
  75. }
  76. }
  77.  
  78. //Print the table footer
  79. $tableOutput.="<TR style='background-color: lightgray;text-align:center;'>";
  80. $tableOutput.="<TD><B>Row</B></TD>";
  81. foreach ($logOriginalHeader as $field)
  82. $tableOutput.="<TD><B>".$field."</B></TD>"; //Add the columns
  83. $tableOutput.="</TR>";
  84.  
  85. //Close the table tag
  86. $tableOutput.="</TABLE>";
  87. }
  88. else $tableOutput = "<b>Invalid data format</b>";
  89. }
  90. else $tableOutput = "<b>Empty file</b>";
  91. }
  92. else $tableOutput = "<b>File not found</b>";
  93. }
  94. else $tableOutput = "<b>Invalid password.</b> Enter the password using this URL format: ".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']."?Password=<b>your_password</b>";
  95.  
  96. ?>
  97. <!DOCTYPE html>
  98. <html>
  99. <head>
  100. <meta charset="UTF-8">
  101. <title>CSV Viewer</title>
  102. </head>
  103. <body>
  104. <h1>CSV Viewer</h1>
  105. <a name="top"></a>
  106. <hr>
  107. <table style="width:50%">
  108. <tr>
  109. <td><a href="" onClick="location.reload()">Refresh</a></td>
  110. <td><a href="<?=$fileName ?>">Download</a></td>
  111. <td><a href="#bottom">End</a></td>
  112. </tr>
  113. </table>
  114. <hr>
  115. <?=$tableOutput ?>
  116. <a name="bottom"></a>
  117. <hr>
  118. <table style="width:50%">
  119. <tr>
  120. <td><a href="" onClick="location.reload()">Refresh</a></td>
  121. <td><a href="<?=$fileName ?>">Download</a></td>
  122. <td><a href="#top">Top</a></td>
  123. </tr>
  124. </table>
  125. <hr>
  126. </body>
  127. </html>
The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.