//system.xml
<file>
<label>Add attachment</label>
<frontend_type>file</frontend_type>
<backend_model>module/system_config_backend_file</backend_model>
<upload_dir config="system/filesystem/var" scope_info="1">file_folder</upload_dir>
<sort_order>210</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</file>
<?php
//Model
class Company_Module_Model_System_Config_Backend_File extends Mage_Core_Model_Config_Data
{
protected function _beforeSave()
{
$value = $this->getValue();
if (is_array($value) && !empty($value['delete'])) {
$filePath = $this->_getUploadDir().DS.$value['value'];
$this->setValue('');
unlink($filePath);
}
if ($_FILES['groups']['tmp_name'][$this->getGroupId()]['fields'][$this->getField()]['value']){
$uploadDir = $this->_getUploadDir();
try {
$file = array();
$tmpName = $_FILES['groups']['tmp_name'];
$file['tmp_name'] = $tmpName[$this->getGroupId()]['fields'][$this->getField()]['value'];
$name = $_FILES['groups']['name'];
$file['name'] = $name[$this->getGroupId()]['fields'][$this->getField()]['value'];
$uploader = new Mage_Core_Model_File_Uploader($file);
$uploader->setAllowedExtensions($this->_getAllowedExtensions());
$uploader->setAllowRenameFiles(true);
$result = $uploader->save($uploadDir);
} catch (Exception $e) {
Mage::throwException($e->getMessage());
return $this;
}
}
return $this;
}
protected function _getUploadDir()
{
$fieldConfig = $this->getFieldConfig();
$uploadDir = (string)$fieldConfig->upload_dir;
$el = $fieldConfig->descend('upload_dir');
if (!empty($el['config'])) {
$uploadRoot = $this->_getUploadRoot((string)$el['config']);
$uploadDir = $uploadRoot . '/' . $uploadDir;
}
return $uploadDir;
}
protected function _getUploadRoot($token)
{
return Mage::getBaseDir('var');
}
protected function _getAllowedExtensions()
{
return array('pdf');
}
}
?>