【问题标题】:upload file in joomla using a module使用模块在joomla中上传文件
【发布时间】:2017-03-21 12:18:01
【问题描述】:

我正在尝试创建一个特定的模块来上传文件。

我正在使用此代码:

客户端:

<?php 
// No direct access
defined('_JEXEC') or die; $resposta =""; ?>
<form name="upload" method="post" enctype="multipart/form-data">
<input type="file" name="file_upload" />
<input type="submit" name="submit_file" value="submit_file"/>
<input type="text" name="resposta" value=<?php echo $resposta; ?> />
</form>

我的模块:

<?php
   defined('_JEXEC') or die;

include_once __DIR__ . '/helper.php';

//trigger the event 


// Instantiate global document object
 defined('_JEXEC') or die;

   // Include the syndicate functions only once
   require_once dirname(__FILE__) . '/helper.php';

   $resposta = ModuploadfileHelper::getuploadfile($params);
   require JModuleHelper::getLayoutPath('mod_upload_file');


?>

我的助手:

<?php


   class ModuploadfileHelper {


      public static function getuploadfile($params) {
         /*
            * File upload example
                                    */
        //Retrieve file details from uploaded file, sent from upload form
        $file = JFactory::getApplication()->input->get('file_upload');

        //Import filesystem libraries. Perhaps not necessary, but does not hurt
        jimport('joomla.filesystem.file');

        //Clean up filename to get rid of strange characters like spaces etc
        $filename = JFile::makeSafe($file['name']);

        //Set up the source and destination of the file
        $src = $file['tmp_name'];
        $dest = JPATH_COMPONENT . DS . "uploads" . DS . $filename;
        if(!JFolder::exists($dest))
        {
            $mode = 0755;
            JFolder::create($dest, $mode);
        }
        $resposta = null;
        //First check if the file has the right extension, we need jpg only
        if (strtolower(JFile::getExt($filename)) == 'jpg') 
        {
        // TODO: Add security checks

            if (JFile::upload($src, $dest))
            {
                $resposta = "Sucesso ao arquivar a imagem";
            }    
            else
            {
                $resposta = "Insucesso ao arquivar a imagem";
            }
        }
        else
        {
            $resposta = "O ficheiro não é uma imagem";
        }
         return $resposta;
      }
   }

?>

第一个问题:这样的事情有用吗? 第二个问题:如何触发模块工作?

第十三题:如何将模块传给ajax? 我有这样的事情:

模块代码:

<?php
   defined('_JEXEC') or die;

include_once __DIR__ . '/helper.php';

// Instantiate global document object
 defined('_JEXEC') or die;

   // Include the syndicate functions only once
   require_once dirname(__FILE__) . '/helper.php';

   $resposta = ModuploadfileHelper::getuploadfile($params);


defined('_JEXEC') or die;

include_once __DIR__ . '/helper.php';

// Instantiate global document object
$doc = JFactory::getDocument();

$js = <<<JS
(function ($) {
    $(document).on('click', 'input[type=submit]', function () {
            formdata = new FormData();
            var file = this.files[0];
            formdata.append("image", file);

        $.ajax({
            type   : 'POST',
            data   : request,
            success: function (response) {
                $('.search-results').html(response);
            }
        });
        return false;
    });
})(jQuery)
JS;


$doc->addScriptDeclaration($js);
require JModuleHelper::getLayoutPath('mod_upload_file');

?>

请帮帮我。

【问题讨论】:

    标签: javascript php ajax file-upload joomla3.0


    【解决方案1】:

    有可能,你需要做的是使用Joomla ajax接口。

    在此处查看文档:https://docs.joomla.org/Using_Joomla_Ajax_Interface

    有一个实现此功能的模块的完整示例,您可以轻松修改以适应文件上传: https://github.com/Joomla-Ajax-Interface/Ajax-Session-Module

    【讨论】:

    • 我做了一些研究,从我读到的一些浏览器不支持通过 ajax 提交表单。我想保持文件上传简单。
    • 我看不到哪个浏览器...但是如果您不想要 ajax,我真的看不出问题,只是您的模块需要发布在表单所在的页面上被发布,所以它被触发并且可以作用于 $_FILES 变量。或者更确切地说 JFactory::getApplication()->input->files->get('file_upload');
    • 更好的是,您甚至可以对 'com_ajax' 模块 url 进行常规发布请求,并在处理上传后进行重定向。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-25
    • 2015-10-21
    • 2012-10-05
    • 2016-10-09
    • 1970-01-01
    • 2014-05-08
    • 1970-01-01
    相关资源
    最近更新 更多