<html>
<! ScanFile.php                                               >
<! Chapter 10, Oracle Database XE PHP Web Programming         >
<! by Michael McLaughlin                                      >
<!                                                            >
<! This script demonstrates how to move a files by copying    >
<! and then deleting the file; and managing a directory       >
<! handle by using the scanfile() function.                   >
<head>
<title>
  Chapter 10 : ScanFile.php
</title>
</head>
<body>
<?php
  // Declare file variables.
  $file1 = "NewFile1.txt";
  $file2 = "NewFile2.txt";

  // Declare array of files.
  $list = array($file1,$file2);

  // Check for file before creating it.
  if (!file_exists($file1))
    touch($file1);

  // Print matching files from directory.
  print "<hr />".find_files($list);

  // Copy first file to second file.
  copy($file1,$file2);

  // Print matching files from directory.
  print "<hr />".find_files($list);

  // Delete first file from directory.
  unlink($file1);

  // Print matching files from directory.
  print "<hr />".find_files($list)."<hr />";

  // Find a list of files in a directory passed by reference.
  function find_files($set)
  {
    // Declare a return string.
    $list = "";

    // Declare an array with the directory files.
    $dir = scandir(getcwd());

    // Read through the list of files.
    for ($i = 0;$i < count($dir);$i++)
    {
	// Check if they're members of the search array.
      if (in_array($dir[$i],$set))
        $list .= $dir[$i]."<br />";
    }
	// Return the formatted string.
	return $list;
  }
?>
</body>
</html>