title: Unable to upload file in CakePHP 2
body: <p>I'm trying to incorporate an upload function in my CakePHP application. I have built one before for a raw PHP project, and decided to reuse that code because I know it works. The code is as follows:</p>
  
  <pre><code>    $allowed_filetypes = array('.jpg','.gif','.bmp','.png');
      $max_filesize = 1000000; // Maximum filesize in BYTES
      $upload_path = './files/';
  
      $filename = $_FILES['userfile']['name'];
      $desiredname = $_POST['desiredname'];
      $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1);
  
      $savedfile = $desiredname.$ext;
  
      // Check if the filetype is allowed, if not DIE and inform the user.
      if(!in_array($ext,$allowed_filetypes))
          die('The file you attempted to upload is not allowed.');
  
      // Now check the filesize, if it is too large then DIE and inform the user.
      if(filesize($_FILES['userfile']['tmp_name']) &gt; $max_filesize)
          die('The file you attempted to upload is too large.');
  
      // Check if we can upload to the specified path, if not DIE and inform the user.
      if(!is_writable($upload_path))
          die('You cannot upload to the specified directory, please CHMOD it to 777.');
  
      // Upload the file to your specified path.
      if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $savedfile))
          echo 'Your file upload was successful, view the file &lt;a href="' . $upload_path . $savedfile . '" title="Your File"&gt;here&lt;/a&gt;'; // It worked.
      else
          echo 'There was an error during the file upload.  Please try again.'; // It failed :(.
  </code></pre>
  
  <p>I've put this code in to the Controller of a page that I want to upload. I have used the FormHelper in CakePHP to generate the form, which is as follows:</p>
  
  <pre><code>    &lt;?php 
          echo $this-&gt;Form-&gt;create('Customer', array(
              'class' =&gt; 'form-horizontal',
              'action' =&gt; 'add',
              'enctype' =&gt; 'multipart/form-data'
          ));
  
          echo $this-&gt;Form-&gt;input('filename', array(
              'type' =&gt; 'text',
              'label' =&gt; 'Filename',
              'class' =&gt; 'span5'
          ));
          echo $this-&gt;Form-&gt;input('file', array(
              'between' =&gt; '&lt;br /&gt;',
              'type' =&gt; 'file'
          ));
          echo $this-&gt;Form-&gt;end('Save Changes', array(
              'label' =&gt; false,
              'type' =&gt; 'submit',
              'class' =&gt; 'btn btn-primary'
          ));
  
          echo $this-&gt;Form-&gt;end();
       ?&gt;
  </code></pre>
  
  <p>I have changed the any references to the fields in my old code to reflect the change of form being used in this project. However, I get the following errors when I submit the form:</p>
  
  <blockquote>
    <p>Notice (8): Undefined index: CustomerFile
    [APP\Controller\CustomersController.php, line 148]</p>
    
    <p>Notice (8): Undefined index: CustomerFilename
    [APP\Controller\CustomersController.php, line 149]</p>
  </blockquote>
  
  <p>In the code in the controller, I have changed the form fields (again) to use the following:</p>
  
  <pre><code>$filename = $this-&gt;request-&gt;data['CustomerFile']['name'];
  $desiredname = $this-&gt;request-&gt;data['CustomerFilename'];
  </code></pre>
  
  <p>But the error still occurs. I'm guessing that the form fields aren't being referenced to properly, but I thought I had referenced them properly using the <code>$this-&gt;request</code> code but obviously it hasn't worked. Has anyone any ideas?</p>