Answers.Body: <p>Yii’s <a href="http://www.yiiframework.com/doc/api/1.1/CUploadedFile" rel="nofollow">CUploadedFile</a> class actually doesn’t support retrieving uploaded files that came from a file input whose name was “namespaced” (e.g. <code>name="fields[handle]"</code> rather than just <code>name="handle"</code>).</p>
  
  <p>However, Craft has its own <a href="http://buildwithcraft.com/classreference/etc/web/UploadedFile" rel="nofollow">UploadedFile</a> class which extends CUploadedFile, and the whole point of it is to add that namespaced input name support. It uses a dot syntax rather than square brackets, so if a file input had the name <code>fields[handle]</code>, you would fetch that file like this:</p>
  
  
  
  <pre class="lang-php prettyprint-override"><code>$file = UploadedFile::getInstanceByName('fields.handle');
  </code></pre>
  
  <p>Problem is, you can’t actually rely on the file input name having a <code>fields</code> namespace – it will vary depending on the form. In fact many element forms within Craft have randomized namespaces (e.g. the Quick Post dashboard widget, or the modal windows that come up when you double-click on an element within a relational field).</p>
  
  <p>That’s why field types have a <a href="http://buildwithcraft.com/classreference/fieldtypes/BaseFieldType#getContentPostLocation-detail" rel="nofollow">getContentPostLocation()</a> method, which returns the input name that would have been used for the current field, namespace and all, converted into dot syntax. So you can plug that value directly into getInstanceByName() and get your uploaded file.</p>
  
  <pre class="lang-php prettyprint-override"><code>$contentPostLocation = $this-&gt;getContentPostLocation();
  
  if ($contentPostLocation)
  {
      $file = UploadedFile::getInstanceByName($contentPostLocation);
  
      // ...
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-05T23:29:28.817Z
Answers.LastActivityDate: 2014-11-05T23:29:28.817Z
Answers.UserDisplayName: Brandon Kelly
Body: <p>I need to upload a file to my server via a custom field type and I'm trying it like this:</p>
  
  <pre><code>    public function prepValueFromPost($value)
  {
      // Get file
      $file = \CUploadedFile::getInstanceByName('fields[handle]');
  
      // Determine folder
      $folder = 'folder/to/upload';
  
      // Ensure folder exists
      IOHelper::ensureFolderExists($folder);
  
      // Get filepath - save in storage folder
      $path = $folder . $file-&gt;getName();
  
      // Save file to Craft's temp folder for later use
      $file-&gt;saveAs($path);
  }
  
  public function getInputHtml($name, $value)
  {   
      return craft()-&gt;templates-&gt;render('_includes/forms/file', array(
          'name'  =&gt; $name,
          'value' =&gt; $value,
      ));
  }
  </code></pre>
  
  <p>This sadly ends up in the following error:</p>
  
  <pre><code>Fatal error: Call to a member function getName() on a non-object in...
  </code></pre>
  
  <p>Does anyone have an idea how to solve this? My file input has the name "fields[handle]" but i also tried "handle" but nothing changes...</p>
  
Tags: <plugin-development><custom-fieldtype><upload>
Title: Custom field type file upload error [Uploaded File Class]
ThreadText: # Custom field type file upload error [Uploaded File Class]
  
  (Asked by: username_0 on 2014-11-05)
  
  I need to upload a file to my server via a custom field type and I'm trying it like this:
  
  ```
      public function prepValueFromPost($value)
  {
      // Get file
      $file = \CUploadedFile::getInstanceByName('fields[handle]');
  
      // Determine folder
      $folder = 'folder/to/upload';
  
      // Ensure folder exists
      IOHelper::ensureFolderExists($folder);
  
      // Get filepath - save in storage folder
      $path = $folder . $file->getName();
  
      // Save file to Craft's temp folder for later use
      $file->saveAs($path);
  }
  
  public function getInputHtml($name, $value)
  {   
      return craft()->templates->render('_includes/forms/file', array(
          'name'  => $name,
          'value' => $value,
      ));
  }
  
  ```
  
  This sadly ends up in the following error:
  
  ```
  Fatal error: Call to a member function getName() on a non-object in...
  
  ```
  
  Does anyone have an idea how to solve this? My file input has the name "fields[handle]" but i also tried "handle" but nothing changes...
  
  **Tags:** plugin-development, custom-fieldtype, upload
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2014-11-05. Score: 4 | Accepted answer
  
  Yii’s [CUploadedFile](http://www.yiiframework.com/doc/api/1.1/CUploadedFile) class actually doesn’t support retrieving uploaded files that came from a file input whose name was “namespaced” (e.g. `name="fields[handle]"` rather than just `name="handle"`).
  
  However, Craft has its own [UploadedFile](http://buildwithcraft.com/classreference/etc/web/UploadedFile) class which extends CUploadedFile, and the whole point of it is to add that namespaced input name support. It uses a dot syntax rather than square brackets, so if a file input had the name `fields[handle]`, you would fetch that file like this:
  
  ```php
  $file = UploadedFile::getInstanceByName('fields.handle');
  
  ```
  
  Problem is, you can’t actually rely on the file input name having a `fields` namespace – it will vary depending on the form. In fact many element forms within Craft have randomized namespaces (e.g. the Quick Post dashboard widget, or the modal windows that come up when you double-click on an element within a relational field).
  
  That’s why field types have a [getContentPostLocation()](http://buildwithcraft.com/classreference/fieldtypes/BaseFieldType#getContentPostLocation-detail) method, which returns the input name that would have been used for the current field, namespace and all, converted into dot syntax. So you can plug that value directly into getInstanceByName() and get your uploaded file.
  
  ```php
  $contentPostLocation = $this->getContentPostLocation();
  
  if ($contentPostLocation)
  {
      $file = UploadedFile::getInstanceByName($contentPostLocation);
  
      // ...
  }
  
  ```
  
  
  ---
  
  