Scripting Languages - Week Six
Misc File Stuff


<!-- File Manip .php -->
<!-- script called by filemanip.html -->

<html>
<head>
   <title>File Manip</title>
</head>
<Body>

<?php

/*************************************************/
/** print the file with line numbers ************/
function print_file ()
{
   echo "<pre>";
   $filelines = file ("/tmp/sdnifty.txt");
   for ($i=0; $i<count($filelines); $i++)
    {
     $line = $filelines[$i];
     echo "$i - $line";
    }
    echo "</pre><hr>";
}

/*************************************************/
function get_string ($fh)
{
   $nextstring = " ";
   $c = fgetc($fh);
   while (trim($c) != '')
    {
     $nextstring .= $c;
     $c = fgetc($fh);
    }
   return $nextstring;
}

/*************************************************/
/*************************************************/

echo "file BEFORE editing";
print_file();

/***** Which radio box did they select *****/
$choice = $_POST['box'];
switch ($choice)
{
   case 'add' :
      $newtext = $_POST['addtext'];          // what is the text to add?
      echo "adding the text $newtext<P>";
      $fh = fopen ("/tmp/sdnifty.txt", "a"); // open for append
      fwrite ($fh, $newtext . "\n");         // write the new text
      fclose ($fh);
      break;


   case 'remove' :
      $remtext = $_POST['removetext'];         // what are we removing?
      echo "removing the text $remtext<P>";
      $filelines = file ("/tmp/sdnifty.txt");  // get contents of file
      $fh = fopen ("/tmp/sdnifty.txt", "w");   // overwrite old file
      foreach ($filelines as $lines)
        {
         $lines = trim ( $lines );             // remove the \n character
         if (strcmp($remtext, $lines) != 0)    // only rewrite okay lines
            fwrite ($fh, $lines . "\n");
        }
      fclose ($fh); // close the file
      break;


   case 'sort1' :
      echo "sorting the file by column one<P>";
      $filelines = file ("/tmp/sdnifty.txt");   // put the file into an array
      sort ($filelines);                        // sort the array
      $fh = fopen ("/tmp/sdnifty.txt", "w");    // overwrite the old file
      foreach ($filelines as $lines)            // print the array
         fwrite ($fh, $lines);
      fclose ($fh);
      break;


   case 'sort2' :
      echo "sorting the file by column two<P>";
      $cmd = "sort -k 2 /tmp/sdnifty.txt";        // call the linux file sort, key = 2
      exec (escapeshellcmd($cmd), $output, $status);
      if ($status)
         echo "Sorting failed<br>";
      else
        {
         $fh = fopen ("/tmp/sdnifty.txt", "w");  // overwrite old data file
         foreach ($output as $line)              // print each line from the linux command
            fwrite ($fh, $line . "\n");
         fclose ($fh);
        }
      break;


   case 'dups' :
      echo "removing duplicates<P>";
      $cmd = "sort /tmp/sdnifty.txt | uniq";    // call the linux uniq command
      exec ($cmd, $output, $status);
      if ($status)
         echo "command failed<br>";
      else
        {
         $fh = fopen ("/tmp/sdnifty.txt", "w"); // overwrite the old file
         foreach ($output as $line)             // write each line into new file
            fwrite ($fh, $line . "\n");
         fclose ($fh);
        }
      break;


   case 'find':
      $find = $_POST['findtxt'];
      $replace = $_POST['reptxt'];
      echo "finding $find and replacing it with $replace<P>";
      $fh = fopen ("/tmp/sdnifty.txt", "r+");     // open for read and write
      while (!feof($fh))
        {
         $nextstr = get_string($fh);              // read next string from file
         if (strcmp(trim($find),trim($nextstr)) == 0)
           {
            $N = -1 - strlen($find);              // if match, move back and write replacement
            fseek ($fh, $N, SEEK_CUR); 
            fwrite ($fh, $replace);
           }
        }
     break;
     
   default :
      echo "unknown action<p>";
      break;
}


echo "File after edits";
print_file();

?>

<P>&nbsp;<P>
<i>Done.</i>
<hr>

</Body>
</html>