Warning: include(../carousel/index/cookiecode): failed to open stream: No such file or directory in /freeola/users/6/4/sr1045546/htdocs/php/files-to-html/tutorial-old.php on line 13

Warning: include(): Failed opening '../carousel/index/cookiecode' for inclusion (include_path='.:/usr/share/pear:/usr/share/php') in /freeola/users/6/4/sr1045546/htdocs/php/files-to-html/tutorial-old.php on line 13

PHP Tutorial One

by Tim Hill

Text to HTML

This article was inspired by this:

From: Alexander Ausserstorfer
Subject: Tool for converting text files to HTML?
Date: Wed, 21 Feb 2018 06:50
Newsgroups: comp.sys.acorn.misc

I'm looking currently for a tool for RISC OS (5) which is able to
convert text files to HTML (free or not free). I'm already doing this
work for my website all by hand but it is always the same.

I ought to point out that this is not a RISC OS specific solution but instead uses PHP. It is platform independent and relies only on you having access to a web server with PHP: RISC OS users can install WebJames+PHP and my examples are checked to work with its implementation of PHP 5.2.2

For this example, the text files could be anything so I will be using a folder containing the Gutenberg etexts of Shakespeare. And why not? To set this up per this example, create a folder in your web directory. Mine is called "PHP-examples". Inside that, I have created another folder "Shakespeare" and have copied 43 etexts into it, from 'ALLS-WEL' to 'WINTER_T'. The more astute among you may realise these came off a RISC OS CD-ROM and purists may wish to add the .txt file extension to each. It doesn't matter what the files are called as the script will read them automatically and I prefer them without the .txt if we can manage without. If you are reading this because you have a bunch of text files to display, use those, obviously. This example is not dependent on the content of the files: it does not edit them in any way and is similar in many ways to Pix and Pickle which use a script which does a similar job for photos.

Let's pause to consider the structure of a script which will generate those 44 HTML files, including an index..

  • Read the names of the text files
  • Write the heading to an HTML file
  • Either
    1. Give a choice of files to display, or
    2. Include one of the files based on that choice
  • Write the footer to the HTML file.

Okay. Let's create a file called shakespeare.php alongside the directory 'Shakespeare' and start writing some code:

This will load the names of the textfiles into an array called '$filenames[]' and sort them alphabetically.

<!-- START File shakespeare.php -->
<?php
          $folder = "Shakespeare";
          $fileh = opendir("$folder");
          while (false !== ($filename = readdir($fileh))) {
                $filenames[] = $filename;
                }
          sort($filenames);

Now let's find out what we are supposed to be displaying. Our script will call itself with a variable 'f' appended to the URL to determine which text file to include; without one, the summary should be shown.

          $choice = $_GET['f'];
          if ($choice == "") { $choice = "index"; }
          // close the PHP for now
    ?>

Let's now look at the section outside and after the PHP which writes the page header:

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">

We can even use a variable to vary the page title, depending on its content, just by dropping in and out of PHP mode briefly:

<title><?php print($folder . ": " . $choice) ?></title>
</head>
<body>

Of course you can add other stuff in the usual way - styles and whatnot - to your HEAD but no need for them in this simple example. Let us now move on to the page BODY and construct the index display

<?php
      if ($choice == "index") {
          // display the index of all files available with links
          $arraysize = sizeof($filenames) - 1;
          for ($i = 2; $i <= $arraysize; $i++) {
              $filei = $filenames[$i];
              print("<a href=\"?f=$filei\">$filei</a><br>\n");
              }

Now we add the 'else' which provides for the occasions when the Index isn't required but a file is chosen

                   else {
                   print("<pre style=\"white-space:pre-wrap;\">\n");
                   include($folder . "/" . $choice);
                   print("</pre>\n");
          	   }
?>
</body></html>

Okay, that's a trivial example because you would no doubt prefer to process the text files you have in order not to have to use the PRE tags. That will be in a later example. Here's the example output from the script: Shakespeare and here is a download of the shakespeare.php script. TL;DR? All you have to do is copy the script somewhere and change the first variable to the relative path to the folder containing any files which will display in a browser. It may not look like much but the text files are at least wrapped in headers and footers and pass HTML5 validation.

Postscript: a few trivial alterations have turned the Shakespeare script into cat.php which, when dropped into any directory will act like a directory display when summoned in a browser, providing a clickable list of the files which are linked to normally. Download cat.php script.