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

Cha*_*gaD 3 php xml model-view-controller joomla joomla2.5

我根据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"/>
Run Code Online (Sandbox Code Playgroud)

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

我在哪里做错了?

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

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

Tec*_*hie 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 );}
Run Code Online (Sandbox Code Playgroud)

  • 很好的答案.不过JRequest已被弃用.您应该使用JFactory :: getApplication() - > input-> get() (2认同)