<html>
<! WriteFileAsArray.php                                      >
<! Chapter 10, Oracle Database XE PHP Web Programming         >
<! by Michael McLaughlin                                      >
<!                                                            >
<! This script demonstrates how to write a file with an array >
<! of strings.                                                >
<head>
<title>
  Chapter 10 : WriteFileAsArray.php
</title>
</head>
<body>
<?php
  // Declare a file name.
  $file = $_GET["p_name"];

  // Determine file path delimiter.
  if (ereg("Win32",$_SERVER["SERVER_SOFTWARE"]))
    $slash = "\\";
  else
    $slash = "/";

  // Declare a fully qualified file name.
  $fname = getcwd().$slash.$file;

  $data = array();
  $data[] = "<p><i>Okay, how to fool them?</i> Foley asked himself. ";
  $data[] = "Give the other guy something he expected to see, and he'd ";
  $data[] = "see it, whether it was really there or not. They wanted ";
  $data[] = "the Soviets to believe that the Rabbit and his family had ";
  $data[] = "... not skipped town, but had ... died?</p>";

  // When the the file exists delete it.
  if (is_file($fname))
    unlink($fname);

  // Calll the function to write an array to a file.
  write_string($fname,$data);

  // Read the whole file into standard out.
  readfile($fname);

  // Define function to write a CSV file.
  function write_string($file,$string)
  {
    // Read the two-dimensional array and write or append to a file.
    file_put_contents($file,$string);
  }
?>
</body>
</html>
