【问题标题】:Add File Uploader to Joomla Admin Component将文件上传器添加到 Joomla 管理组件
【发布时间】:2012-10-06 05:39:20
【问题描述】:

我根据 Joomla 指南制作了 Joomla 管理组件 - http://docs.joomla.org/Developing_a_Model-View-Controller_Component/2.5/Developing_a_Basic_Component

我需要有文件上传器,让用户上传单个文件。

在 administrator\components\com_invoicemanager\models\forms\invoicemanager.xml 我已经定义了

<field name="invoice" type="file"/>

在控制器 administrator\components\com_invoicemanager\controllers\invoicemanager.php 中,我试图检索该文件,如下所示。但它不起作用(无法检索文件)

我哪里做错了?

如何获取文件并将其保存在磁盘上?

class InvoiceManagerControllerInvoiceManager extends JControllerForm
{
    function save(){
        $file = JRequest::getVar( 'invoice', '', 'files', 'array' );
        var_dump($file);
        exit(0);
    }
}

【问题讨论】:

    标签: php xml model-view-controller joomla joomla2.5


    【解决方案1】:

    要从组件上传文件,您需要在控制器文件中编写代码,并且可以扩展 save() 方法。检查下面给出的代码 -

    public function save($data = array(), $key = 'id')
    {
        // Neccesary libraries and variables
        jimport('joomla.filesystem.file');
    
        //Debugging
        ini_set("display_error" , 1);
        error_reporting(E_ALL);
    
        // Get input object
        $jinput = JFactory::getApplication()->input;
    
        // Get posted data
        $data  = $jinput->get('jform', null, 'raw');
        $file  = $jinput->files->get('jform');
    
        // renaming the file
        $file_ext=explode('.',JFile::makeSafe($file['invoice']['name'])); // invoice - file handler name
        $filename = round(microtime(true)) . '.' . strtolower(end($file_ext));
        // Move the uploaded file into a permanent location.
        if ( $filename != '' ) {
    
            // Make sure that the full file path is safe.
            $filepath = JPath::clean( JPATH_ROOT."/media/your_component_name/files/". $filename );
    
            // Move the uploaded file.
            if (JFile::upload( $file['invoice']['tmp_name'], $filepath )) {
                  echo "success :)";
            } else {
                  echo "failed :(";
            }
    
            $data['name'] =  $filename ; // getting file name
            $data['path'] =  $filepath ; // getting file path
            $data['size'] =  $file['invoice']['size'] ; // getting file size
        }
        JRequest::setVar('jform', $data, 'post');
        $return = parent::save($data);
        return $return;
    }
    

    【讨论】:

      【解决方案2】:

      确保您在提交文件的表单中包含enctype="multipart/form-data"。这是一个常见的错误

      /// Get the file data array from the request.
      $file = JRequest::getVar( 'Filedata', '', 'files', 'array' ); 
      
      /// Make the file name safe.
      jimport('joomla.filesystem.file');
      $file['name'] = JFile::makeSafe($file['name']);
      
      /// Move the uploaded file into a permanent location.
      if (isset( $file['name'] )) {
      
      /// Make sure that the full file path is safe.
      $filepath = JPath::clean( $somepath.'/'.strtolower( $file['name'] ) );
      
      /// Move the uploaded file.
      JFile::upload( $file['tmp_name'], $filepath );}
      

      【讨论】:

      • 谢谢@Dasun。获取文件数据应该是这样的。 $file = JRequest::getVar('jform', null, 'files', 'array');
      • 另外,如果您阅读 joomla 文档,您会在那里找到相同的代码 :-)
      • 很好的答案。 JRequest 已被弃用。您应该改用 JFactory::getApplication()->input->get()
      • @SørenBeckJensen 的评论有点不正确。正确的替换是$file = JFactory::getApplication()-&gt;input-&gt;files-&gt;get('jform');。更多信息可以在documentation for JInput找到。
      【解决方案3】:

      http://docs.joomla.org/How_to_use_the_filesystem_package

      有一个完整的上传示例。

      管理员选择文件类型或全部的小示例,输入用户以访问表单上传。 Joomla 目录或绝对路径上传文件的文件夹。只有选定的用户才能访问表单上传。

      【讨论】:

        【解决方案4】:

        Joomla 2.5 & 3 风格:

        $app = JFactory::getApplication();
        $input = $app->input;
        $file= $input->files->get('file');
        
        if(isset($file['name']))
        {
            jimport('joomla.filesystem.file');
            $file['name'] = strtolower(JFile::makeSafe($file['name']));
            $fileRelativePath = '/pathToTheRightFolder/'.$file['name'];
            $fileAbsolutePath = JPath::clean( JPATH_ROOT.$fileRelativePath);
            JFile::upload( $file['tmp_name'], $fileAbsolutePath );
        }
        

        【讨论】:

          【解决方案5】:

          我认为我找到了解决方案:)

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

          这里提到了保存部分-http://docs.joomla.org/Secure_coding_guidelines

          【讨论】:

            猜你喜欢
            • 2012-09-30
            • 2011-04-02
            • 1970-01-01
            • 2012-06-14
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-10-29
            • 2011-04-11
            相关资源
            最近更新 更多