<html>
<! UploadFile.php                                             >
<! Chapter 10, Oracle Database XE PHP Web Programming         >
<! by Michael McLaughlin                                      >
<!                                                            >
<! This demonstrates uploading, moving an uploaded file and   >
<! rendering the content of the uploaded file.                >
<head>
<title>
  Chapter 10 : UploadFile.php
</title>
</head>
<body>
<?php
  // Declare and format $_FILES array elements into HTML table.
  $out = "<table border=0 cellpadding=0 cellspacing=0>";
  foreach ($_FILES['userfile'] as $name => $value)
    $out .= "<tr><td>[$name]</td><td>[$value]</td></tr>";
  $out .= "</table>";

  // Display array elements to web page.
  print $out;

  // Declare a variable for file contents.
  $contents = "";

  // Define the upload file name for Windows or Linux.
  if (ereg("Win32",$_SERVER["SERVER_SOFTWARE"]))
    $uploadFile = getcwd()."\\temp\\".$_FILES['userfile']['name'];
  else
    $uploadFile = getcwd()."/temp/".$_FILES['userfile']['name'];

  // Check for and move uploaded file.
  if (is_uploaded_file($_FILES['userfile']['tmp_name']))
    move_uploaded_file($_FILES['userfile']['tmp_name'],$uploadFile);

  // Open a file handle and suppress an error for a missing file.
  if ($fp = @fopen($uploadFile,"r"))
  {
    // Read until the end-of-file marker.
    while (!feof($fp))
      $contents .= fgetc($fp);

    // Close an open file handle.
    fclose($fp);
  }

  // Displayed moved file in web page.
  print $contents;
?>
</body>
</html>