<p>After some more digging I think I found the problem with my approach:</p>
<p>Craft sends <em>all</em> changes in a form directly after editing via AJAX to save as a draft and on <em>save</em> only publishes the last draft.
That's the reason why $_FILES or $_POST for that matter does not contain all information of the form.</p>
<p>Every &quot;normal&quot; field which can be added to a backend form has this built in and will send this ajax-request to save the change one made.
The file field however does not.
I guess this has to do with the way Craft uses their own buttons for uploading assets.</p>
<p>The problem is not solved, but I've since switched to a different approach where I use a custom volume and asset field for my upload and another field which can react to the saved asset in the other field.</p>

CC BY-SA 4.0
2022-03-22T13:35:57.283Z
2022-03-22T13:35:57.283Z
Aram Loosman
<p>I am working on a plugin which adds a custom field type.</p>
<p>The field type should allow the upload of a file, which is then sent to another server (thus not a normal asset, as it is only temporarily stored on the server).
My goal is to save the file and store the path and some metadata as JSON in field type column.</p>
<p>I have made a plugin with a custom field type which works when I am using a text input field.</p>
<p>But changing the type to a file upload does nothing.</p>
<p>I've seen the following solution on SE: <a href="https://craftcms.stackexchange.com/a/28253/11621">https://craftcms.stackexchange.com/a/28253/11621</a>
but the UploadedFile Class always returns &quot;null&quot;.</p>
<pre><code>public function serializeValue($value, ElementInterface $element = null)
{
    $namespace = $element? $element-&gt;getFieldParamNamespace() . '.' : '';
    $fullhandle = $namespace.$this-&gt;handle;

    $file = UploadedFile::getInstanceByName($fullhandle);
    dd($file);
}
</code></pre>
<p>The variable <code>$fullhandle</code> contains the value <code>fields.introductionVideo</code> which matches the form input's name of <code>fields[introductionVideo]</code>.
The form has the attribute for file uploads <code>enctype=&quot;multipart/form-data&quot;</code> for file uploads.</p>
<p>Does anybody have a clue what might be wrong or where to find more information/working example about how to do it correctly?</p>

<craft3><custom-fieldtype>
Custom Field Types File upload returns null
# Custom Field Types File upload returns null

(Asked by: username_0 on 2022-03-08)

I am working on a plugin which adds a custom field type.
The field type should allow the upload of a file, which is then sent to another server (thus not a normal asset, as it is only temporarily stored on the server).
My goal is to save the file and store the path and some metadata as JSON in field type column.
I have made a plugin with a custom field type which works when I am using a text input field.
But changing the type to a file upload does nothing.
I've seen the following solution on SE: https://craftcms.stackexchange.com/a/28253/11621
but the UploadedFile Class always returns "null".
public function serializeValue($value, ElementInterface $element = null)
{
    $namespace = $element? $element->getFieldParamNamespace() . '.' : '';
    $fullhandle = $namespace.$this->handle;

    $file = UploadedFile::getInstanceByName($fullhandle);
    dd($file);
}

The variable $fullhandle contains the value fields.introductionVideo which matches the form input's name of fields[introductionVideo].
The form has the attribute for file uploads enctype="multipart/form-data" for file uploads.
Does anybody have a clue what might be wrong or where to find more information/working example about how to do it correctly?

**Tags:** craft3, custom-fieldtype

---

## 1 answers

### Answer by username_0 on 2022-03-22. Score: 1

After some more digging I think I found the problem with my approach:

Craft sends *all* changes in a form directly after editing via AJAX to save as a draft and on *save* only publishes the last draft.
That's the reason why $\_FILES or $\_POST for that matter does not contain all information of the form.

Every "normal" field which can be added to a backend form has this built in and will send this ajax-request to save the change one made.
The file field however does not.
I guess this has to do with the way Craft uses their own buttons for uploading assets.

The problem is not solved, but I've since switched to a different approach where I use a custom volume and asset field for my upload and another field which can react to the saved asset in the other field.


---

