将文件上传器添加到Joomla Admin Component

时间:2012-10-16 07:13:49

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

根据Joomla指南 - http://docs.joomla.org/Developing_a_Model-View-Controller_Component/2.5/Developing_a_Basic_Component

,我制作了Joomla管理组件

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

在管理员\ components \ com_invoicemanager \ models \ forms \ invoicemanager.xml中我定义了

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

在控制器管理员\ components \ com_invoicemanager \ controllers \ invoicemanager.php中尝试检索该文件,如下所示。但它无法正常工作(无法检索文件)

我在哪里做错了?

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

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

5 个答案:

答案 0 :(得分:6)

确保您在文件提交的表单中包含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 );}

答案 1 :(得分:1)

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

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

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

答案 2 :(得分: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;
}

答案 3 :(得分:0)

Joomla 2.5&amp; 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 );
}

答案 4 :(得分:0)

http://docs.joomla.org/How_to_use_the_filesystem_package

有完整的上传示例。

  

管理员选择文件类型或全部的小样本,输入用户访问表单上传。用于在Joomla目录中或使用绝对路径上载文件的文件夹。只有选定的用户才能访问表单上传。