title: PHP form -- check if file was attached
body: <p>I have a php contact form that currently validates an attachment for file type and file size, <em>but</em> I also need it to check if the user forgot to attach an image altogether (ie, photo attachment should be a "required field" and throw an 'please attach photo' message if so).</p>
  
  <p>Form in question:</p>
  
  <p><a href="http://www.cucinadellanonna.com/nominate-form.php" rel="nofollow">http://www.cucinadellanonna.com/nominate-form.php</a></p>
  
  <p>This is the part of the code that checks the attachment... I've tried adding a few things, to no avail, so I'm just stabbing in the dark.</p>
  
  <pre><code>//file upload validation
  
  
       if(!empty($this-&gt;fileupload_fields))
      {
       if(!$this-&gt;ValidateFileUploads())
       {
          $ret = false;
       }
      }
      return $ret;
  }
  
  function ValidateFileType($field_name,$valid_filetypes)
  {
      $ret=true;
      $info = pathinfo($_FILES[$field_name]['name']);
      $extn = $info['extension'];
      $extn = strtolower($extn);
  
      $arr_valid_filetypes= explode(',',$valid_filetypes);
      if(!in_array($extn,$arr_valid_filetypes))
      {
          $this-&gt;add_error("Valid file types are: $valid_filetypes");
          $ret=false;
      }
      return $ret;
  }
  
  function ValidateFileSize($field_name,$max_size)
  {
      $size_of_uploaded_file = 
              $_FILES[$field_name]["size"]/1024;//size in KBs
      if($size_of_uploaded_file &gt; $max_size)
      {
          $this-&gt;add_error("The file is too big. File size should be less than $max_size KB");
          return false;
      }
      return true;
  }
  
  function IsFileUploaded($field_name)
  {
      if(empty($_FILES[$field_name]['name']))
      {
          return false;
      }
      if(!is_uploaded_file($_FILES[$field_name]['tmp_name']))
      {
          return false;
      }
      return true;
  }
  function ValidateFileUploads()
  {
      $ret=true;
      foreach($this-&gt;fileupload_fields as $upld_field)
      {
          $field_name = $upld_field["name"];
  
          $valid_filetypes = $upld_field["file_types"];
  
          if(!$this-&gt;IsFileUploaded($field_name))
          {
              continue;
          }
  
          if($_FILES[$field_name]["error"] != 0)
          {
              $this-&gt;add_error("Error in file upload; Error code:".$_FILES[$field_name]["error"]);
              $ret=false;
          }
  
          if(!empty($valid_filetypes) &amp;&amp;
           !$this-&gt;ValidateFileType($field_name,$valid_filetypes))
          {
              $ret=false;
          }
  
          if(!empty($upld_field["maxsize"]) &amp;&amp;
          $upld_field["maxsize"]&gt;0)
          {
              if(!$this-&gt;ValidateFileSize($field_name,$upld_field["maxsize"]))
              {
                  $ret=false;
              }
          }
  
      }
      return $ret;
  }
  </code></pre>