2012-12-12 50 views
2

我在创建自定义字段的上传机制时遇到了困难:libraries/joomla/filesystem/file.php。该表单在视图中设置了正确的enctype,它只是不上传。下面是我的组件/模型/场/ uploadfield.php代码:Joomla自定义组件上传表单

protected function getInput() 
{ 

    //Retrieve file details from uploaded file, sent from upload form 
    $file = JFactory::getApplication()->input->get($this->name, null, 'files', 'array'); 

    //Import filesystem libraries. Perhaps not necessary, but does not hurt 
    jimport('joomla.filesystem.file'); 

    //Clean up filename to get rid of strange characters like spaces etc 
    $filename = JFile::makeSafe($file['name']); 

    //Set up the source and destination of the file 
    $src = $file['tmp_name']; 
    $dest = JPATH_COMPONENT . DIRECTORY_SEPARATOR . $filename; 

    //First check if the file has the right extension, we need jpg only 
    if (strtolower(JFile::getExt($filename)) == 'jpg') { 
     if (JFile::upload($src, $dest)) { 
     //Redirect to a page of your choice 
     } else { 
     //Redirect and throw an error message 
     } 
    } else { 
    //Redirect and notify user file is not right extension 
    } 

    return '<input type="file" name="' . $this->name . '" id="' . $this->id . '"' . ' />'; 
} 

上午我甚至会对此具有处于getInput()函数上传机制的正确方法?它应该在模型中吗?我真的坚持如何使这项工作,一直试图遵循:http://docs.joomla.org/How_to_use_the_filesystem_package但它忽略了上传代码应该去哪里?

非常感谢

回答

1

好了 - 所以我解决了这个问题:

我使用的getInput()函数的自定义字段文件只是做表格上显示的输入域的工作,并在控制器中创建一个函数来完成节约。去有点像这样:

function save(){ 
    jimport('joomla.filesystem.file'); 

    $jFileInput = new JInput($_FILES); 
    $theFiles = $jFileInput->get('jform',array(),'array'); 

    $filepath = "DESTINATIONFILEPATH" 
    JFile::upload($theFiles['tmp_name']['filename'], $filepath); 

    return parent::save(); 
} 

只需要制定的文件名保存到JInput“jform”阵列,这样的文件名获取的更新到数据库的Joomla 3.0的方式。这只是覆盖现有jform数据:

$jinput = JFactory::getApplication()->input; 
$jinput->set('jform',$arraytoadd); 

我在这里问的问题,如果有人有兴趣: Adding field input to JForm using JInput

3

尝试使用这就是我对我的部件之一使用了以下:

function getInput(){ 

    jimport('joomla.filesystem.file'); 

    $jinput = JFactory::getApplication()->input; 
    $fileInput = new JInput($_FILES); 
    $file = $fileInput->get('image', null, 'array'); 

    if(isset($file) && !empty($file['name'])) { 
     $filename = JFile::makeSafe($file['name']); 
     $src = $file['tmp_name']; 
     $data['image']=$filename; 

     $dest = JPATH_COMPONENT . '/' . $filename; 

     if (strtolower(JFile::getExt($filename)) == 'jpg') { 
      if(!JFile::upload($src, $dest)) { 
      return false; 
      } 
     } 
     else { 
      JError::raiseWarning('', 'File type not allowed!.'); 
      return false; 
     } 
    } 
    return true; 
} 

请注意,下面的代码:

$file = JRequest::getVar('image', null, 'files', 'array');

图片“来自输入字段的名称,如下所示:

<input type="file" id="" name="image" /> 

因此,改为任何名称给你的输入字段。

+0

你好,非常感谢您的答复 - 我已经试过这一点,它不似乎工作 - 该字段出现在视图上,我可以选择一个文件,但似乎并没有在任何地方上传?我使用Joomla 3.0,所以JRequest :: getVar现在已经被替换为:JFactory :: getApplication() - > input-> get()。 – mousebat

+0

啊,我不知道你在使用Joomla 3.0。我会尝试进行必要的更改 – Lodder

+0

@ user1717113 - 我已更新我的答案 – Lodder