title: Undefined index: file
body: <p>I am getting the undefined index error as I come for the first time in my upload form page or if I move to next page and click on back button then I have the same error message.
  If I upload a file then it works fine and the error message gets away.</p>
  
  <p>I have also tried this:</p>
  
  <pre><code>global $file;
  if (!isset($file)) {
      $file = '';
  }
  </code></pre>
  
  <p>Here is my code:</p>
  
  <pre><code>&lt;form id="uploadForm" name="upload" enctype="multipart/form-data"/&gt;
    &lt;fieldset&gt;
      &lt;input type="hidden" name="MAX_FILE_SIZE" value="30000" /&gt;
      &lt;input type="file" name="file" /&gt;
  &lt;?php
  echo '&lt;pre&gt;';
  var_dump($_REQUEST['file']);
  echo '&lt;/pre&gt;';
  
  $uploaded = new upload;
  //set Max Size
  $uploaded-&gt;set_max_size(350000);
  //Set Directory
  $uploaded-&gt;set_directory("data");
  //Set Temp Name for upload.
  $uploaded-&gt;set_tmp_name($_FILES['file']['tmp_name']);
  //Set file size
  $uploaded-&gt;set_file_size($_FILES['file']['size']);
  //set file type
  $uploaded-&gt;set_file_type($_FILES['file']['type']);
  //set file name
  $uploaded-&gt;set_file_name($_FILES['file']['name']);
  //start copy process
  $uploaded-&gt;start_copy();
  if($uploaded-&gt;is_ok()) 
      echo " upload is doen.";
  else
      $uploaded-&gt;error()."&lt;br&gt;";
  ?&gt;
      &lt;div class="filesize"&gt;JPG minimaal 800x60 pixels max. 2Mb&lt;/div&gt;
      &lt;a href="" class="submit" title="Upload your own phooto"&gt;&lt;span&gt; Upload your own photo &lt;/span&gt;&lt;/a&gt;
  </code></pre>
  
  <p>upload_inc.php</p>
  
  <pre><code>&lt;?
  class upload
  {
      var $directory_name;
      var $max_filesize;
      var $error;
  
      var $user_tmp_name;
      var $user_file_name;
      var $user_file_size;
      var $user_file_type;
      var $user_full_name;
  
      function set_directory($dir_name =".")
      {
          $this-&gt;directory_name = $dir_name;
      }
  
      function set_max_size($max_file = 2000000)
      {
          $this-&gt;max_filesize = $max_file;
      }
  
      function error()
      {
          return $this-&gt;error;
      }
  
      function is_ok()
      {
          if(isset($this-&gt;error))
              return FALSE;
          else
              return TRUE;
      }
  
      function set_tmp_name($temp_name)
      {
          $this-&gt;user_tmp_name = $temp_name;
      }
  
      function set_file_size($file_size)
      {
           $this-&gt;user_file_size = $file_size;
      }
  
      function set_file_type($file_type)
      {
          $this-&gt;user_file_type = $file_type;
      }
  
      function set_file_name($file)
      {
          $this-&gt;user_file_name = $file;
          $this-&gt;user_full_name = $this-&gt;directory_name."/".$this-&gt;user_file_name;
      }
  
      function start_copy()
      {
          if(!isset($this-&gt;user_file_name))
              $this-&gt;error = "You must define filename!";
          if ($this-&gt;user_file_size &lt;= 0)
              $this-&gt;error = 'File size error (0):' . $this-&gt;user_file_size . 'KB &lt;br&gt;';
          if ($this-&gt;user_file_size &gt; $this-&gt;max_filesize)
              $this-&gt;error = 'File size error (1):' . $this-&gt;user_file_size . 'KB&lt;br&gt;';
          if($this-&gt;user_file_type != "image/jpeg")
              $this-&gt;error = "the image must be jpeg extension";
          if (!isset($this-&gt;error))
          {
              $filename = basename($this-&gt;user_file_name);
              if (!empty($this-&gt;directory_name))
                  $destination = $this-&gt;user_full_name;
              else
                  $destination = $filename;
              if(!is_uploaded_file($this-&gt;user_tmp_name))
                  $this-&gt;error = "File " . $this-&gt;user_tmp_name . " is not uploaded correctly.";
              if (!move_uploaded_file ($this-&gt;user_tmp_name,$destination))
                  $this-&gt;error = "Impossible to copy " . $this-&gt;user_file_name . " from your folder to destination directory.";
          }
      }
  
  }
  ?&gt;
  </code></pre>