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...