<html>
<head><title></title></head>
<body onLoad="document.MyForm.Password.focus()">
<?php

// Uploading files.

echo <<< text

<center>
<h1>Uploading Files</h1>
<form enctype="multipart/form-data" action="{$_SERVER['PHP_SELF']}" method=post name="MyForm">
  <!-- You need enctype="multipart/form-data" above -->
  Password: <input type="password" name="Password"><P>
  <input type="hidden" name="submitted" value="TRUE">

<!-- The name of the field below has to be as written below,
and it must be there; also make it hidden -->

  <input type="hidden" name="MAX_FILE_SIZE" value="1000000"><P>

<!-- You need the type to be "file". -->

  Browse file to upload: <input name="MyFile" type="file"><P>

<input type="submit" value="Upload">

<!-- You can replace the submit button by an image -->
 
<!-- <input type="image" src="207.GIF"> 
<input type="image" src="241.GIF">
-->
</form>

text;

/*
Values stored in $_FILES['MyFile']

$_FILES['MyFile']['tmp_name'] is the place where the file has been temporarily stored on the 
web server. It's usually stored in the server's main temporarily directory usually called "". If you don't move 
the file from there before the script finishes execution, it will be deleted.

$_FILES['MyFile']['name'] is the file's name on the user's/client's system.

$_FILES['MyFile']['size'] is the size of the file in bytes.

$_FILES['MyFile']['type'] is the MIME type of the file (see below).

$_FILES['MyFile']['error'] is the error code resulting from the file upload. The possible values are:

UPLOAD_ERROR_OK (has a value of 0): no error occured.
UPLOAD_ERR_INI_SIZE (Value 1) Size of uploaded file exceeds the amount specified in upload_max_filesize directive 
UPLOAD_ERR_FORM_SIZE (Value 2) Size of uploaded file exceeds the amount specified in the MAX_FILE_SIZE in the HTML 
form 
UPLOAD_ERR_PARTIAL (Value 3) The file was partially uploaded 
UPLOAD_ERR_NO_FILE (Value 4) No file was uploaded
*/

if (isset($_POST['submitted']))
{
 if ($_POST['Password']=="FALL")
 {
  if ($_FILES['MyFile']['error'] > 0)
  {
    switch ($_FILES['MyFile']['error'])
    {
      case 1:  echo 'WARNING: File exceeded upload_max_filesize';  break;
      case 2:  echo 'WARNING: File exceeded max_file_size';  break;
      case 3:  echo 'WARNING: File only partially uploaded';  break;
      case 4:  echo 'WARNING: No file uploaded';  break;
    }
    exit;
  }

  // Check if the type of the file to be uploaded is ok
  // Type: No difference between lowercase and uppercase
  $valid = array('image/gif','image/jpg','image/jpeg','image/png','image/bmp',
	'text/html','application/pdf', 'application/cpp', 
	'application/msword','text/plain', 'text/rtf', 
	'text/css');
  //if ($_FILES['MyFile']['type'] != 'text/plain')
  if (!in_array($_FILES['MyFile']['type'], $valid))
  {
    echo '<h2><font color=red>';
    echo 'WARNING: this type of files is not allowed';
    echo '</font></h2>';
    exit;
  }

  // put the file where we'd like it to be
  $file_to_upload = "/home/abu-jeib/temp/{$_FILES['MyFile']['name']}";

// if ( isset($_POST['MyFile']) ) // NO
// if (isset($_Files['MyFile'])) // NO
  if (is_uploaded_file($_FILES['MyFile']['tmp_name'])) 
  {
	// echo $_FILES['MyFile']['tmp_name'], "<P>";
     if (!move_uploaded_file($_FILES['MyFile']['tmp_name'], $file_to_upload))
     {
        echo "Can't move file to destination directory";
        exit;
     }
  } 
  else 
  {
    echo "Problem uploading {$_FILES['MyFile']['name']}";
    exit;
  }
  echo '<font color=blue><h2 align=center>File has been uploaded</h2></font><br><br>'; 
  chmod($file_to_upload,0644); // Needed
 // PHP could be dengerous
  //chown($file_to_upload,"abu-jeib"); // You can do that, but does not work sometimes
 // system("chown -r abu-jeib:users $file_to_upload"); // You can do that, but does not work sometimes
  // copy($file_to_upload,$file_to_upload._bak); // Works, owner still nobody
 // exec("mkdir /home/abu-jeib/temp/new"); // You can do that
					   // If exists, warning.
 // mkdir("/home/abu-jeib/temp/new",0755); // You can do that
// rmdir("/home/abu-jeib/temp/new"); // You can do that for Unix
// rmdir("c:\\temp\\new"); // You can do that for Windows


  $f = fopen($file_to_upload,'r');
  if (!$f)
  {
	echo "Can't open file<BR>\n";
	exit;
  }

/* 
 // Read the whole file and put it in $s.
  $s = fread($f,filesize($file_to_upload));
 // $s =readfile($file_to_upload); // You can also use readfile
 // Show what was uploaded
  echo $s;
  fclose($f);
*/

 /* $s = strip_tags($s);
  $f = fopen($file_to_upload,'w');
  fwrite($f,$s);
  fclose($f);
*/

}
else
	echo "<h2><font color=red>Wrong Password.</font></h2>\n";
}

  // reformat the file contents and display them


?>
</center>
</body>
</html>

