title: Limit File Types in CakePHP File Input
body: <p>I just wanted to know if there is a way to limit the file input dialog to show only certain kinds of files.  My web page only can accept .bin or .gz file types, but the user can select other file types and try to upload them.</p>
  
  <p>What would be the best way to prevent the wrong kind of file from being uploaded?</p>
  
  <p>Here is my controller for file uploading:</p>
  
  <pre><code>    public function uploadFile()
      {
          $this-&gt;Session-&gt;write('isFileUpload', false);
          $this-&gt;Session-&gt;write('isFileLarge', false);
  
          if($this-&gt;request-&gt;is('post'))
          {
              $uploadedFile = array();
  
              // Check if the Document object is set
              // If it is set, process the file for uploading,
              if(isset($this-&gt;request-&gt;data['Document']))
              {
                  $filename = $this-&gt;request-&gt;data['Document']['MyFile']['tmp_name'];
  
                  $uploadedFile['MyFile']['name'] = $this-&gt;request-&gt;data['Document']['MyFile']['name'];
                  $uploadedFile['MyFile']['type'] = $this-&gt;request-&gt;data['Document']['MyFile']['type'];
                  $uploadedFile['MyFile']['size'] = $this-&gt;request-&gt;data['Document']['MyFile']['size'];
  
                  // Move the file to the /home/spectracom folder
                  $filePath = DS . 'home' . DS . $uploadedFile['MyFile']['name'];
  
                  if (move_uploaded_file($filename, $filePath))
                  {
                      $this-&gt;Session-&gt;write('isFileUpload', true);
                      $this-&gt;Session-&gt;write('isFileLarge', false);
                      $this-&gt;redirect('/tools/upgradebackup');
                  }
                  else
                  {
                      $this-&gt;Session-&gt;write('isFileUpload', false);
                      $this-&gt;Session-&gt;write('isFileLarge', true);
                      $this-&gt;redirect('/tools/upgradebackup');
                  }
              }
              else
              {
                  $this-&gt;Session-&gt;write('isFileUpload', false);
                  $this-&gt;Session-&gt;write('isFileLarge', true);
                  $this-&gt;redirect('/tools/upgradebackup');
              }
          }
      }
  </code></pre>
  
  <p>I basically check that the file exists, or else it is too large, and when it returns to the main upgrade page it sets the session variables.</p>
  
  <p>Thanks</p>