Php 将文件上传器添加到Joomla管理组件

Php 将文件上传器添加到Joomla管理组件,php,xml,model-view-controller,joomla,joomla2.5,Php,Xml,Model View Controller,Joomla,Joomla2.5,我根据Joomla指南制作了Joomla管理组件- 在这方面,我需要有文件上传,让用户上传单个文件 在administrator\components\com\u invoicemanager\models\forms\invoicemanager.xml中,我定义了 <field name="invoice" type="file"/> 我想我找到了解决办法:) 此处提到了保存部分-请确保在提交文件的表单中包含了enctype=“multipart/form data”。这是一个

我根据Joomla指南制作了Joomla管理组件-

在这方面,我需要有文件上传,让用户上传单个文件

在administrator\components\com\u invoicemanager\models\forms\invoicemanager.xml中,我定义了

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

我想我找到了解决办法:)


此处提到了保存部分-

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

有一个完整的上传样本

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


要从组件上载文件,需要在控制器文件中编写代码,并且可以扩展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;
}

谢谢@Dasun。获取文件数据应该是这样的$file=JRequest::getVar('jform',null,'files','array');另外,如果你阅读joomla文档,你会在那里找到相同的代码:-)很好的答案。但JRequest已被弃用。您应该使用JFactory::getApplication()->input->get()instead@SørenBeckJensen的评论有点不正确。正确的替换是
$file=JFactory::getApplication()->input->files->get('jform')。更多信息可在中找到。
$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 );
}
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;
}