3

我根据 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"/>

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

我在哪里做错了?

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

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

5 回答 5

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 );}
于 2012-10-16T07:41:31.713 回答
1

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

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

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

于 2012-10-16T07:39:30.173 回答
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;
}
于 2016-03-14T13:18:04.583 回答
0

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 );
}
于 2013-10-17T15:47:22.630 回答
0

http://docs.joomla.org/How_to_use_the_filesystem_package

有一个完整的上传示例。

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

于 2014-06-04T08:01:59.713 回答