【问题标题】:Joomla - how to add abutton upload file to parameter of template and do upload file?Joomla - 如何将按钮上传文件添加到模板参数并上传文件?
【发布时间】:2011-04-11 01:22:33
【问题描述】:

我自定义了一个参数添加按钮上传<input type="file" name="file">

但是我们在com_template的时候怎么上传

【问题讨论】:

    标签: joomla


    【解决方案1】:

    您需要进行一些调整才能使上传正常工作。

    1. 将表单的编码类型更改为enctype="multipart/form-data"
    2. 如果使用 MVC 架构,请在表单中添加 taskcontroller

    上传文件非常容易,因为 Joomla 有 filesystem 包。上传文件只需致电JFile::upload($src, $dest)

    阅读文件系统包,你会发现很多有用的东西。这是链接http://docs.joomla.org/How_to_use_the_filesystem_package

    这是上传代码的样子(来自 Joomla 文档)

    /**
     * Uploading function for Joomla
     * @param int $max maximum allowed site of file
     * @param string $module_dir path to where to upload file
     * @param string $file_type allowed file type
     * @return string response message
     */    
    function fileUpload($max, $module_dir, $file_type){
            //Retrieve file details from uploaded file, sent from upload form
            $file = JRequest::getVar('file_upload', null, 'files', 'array'); 
            // Retorna: Array ( [name] => mod_simpleupload_1.2.1.zip [type] => application/zip 
            // [tmp_name] => /tmp/phpo3VG9F [error] => 0 [size] => 4463 ) 
    
            if(isset($file)){ 
                    //Clean up filename to get rid of strange characters like spaces etc
                    $filename = JFile::makeSafe($file['name']);
    
                    if($file['size'] > $max) $msg = JText::_('ONLY_FILES_UNDER').' '.$max;
                    //Set up the source and destination of the file
    
                    $src = $file['tmp_name'];
                    $dest = $module_dir . DS . $filename;
    
                    //First check if the file has the right extension, we need jpg only
                    if ($file['type'] == $file_type || $file_type == '*') { 
                       if ( JFile::upload($src, $dest) ) {
    
                           //Redirect to a page of your choice
                            $msg = JText::_('FILE_SAVE_AS').' '.$dest;
                       } else {
                              //Redirect and throw an error message
                            $msg = JText::_('ERROR_IN_UPLOAD');
                       }
                    } else {
                       //Redirect and notify user file is not right extension
                            $msg = JText::_('FILE_TYPE_INVALID');
                    }
    
            }
            return $msg;
    }
    

    【讨论】:

    • 它应该进入控制器。您必须为表单的提交操作注册控制器事件或将其更改为受保护/私有方法并在控制器内调用
    • 如何调用函数?每次我把它放在控制器中然后尝试调用它都会给我一个错误,例如。类 MJobsControllerMJob 扩展 JControllerForm { private function test1(){ echo "这是在控制器测试中"; } test1();解析错误:语法错误,意外的 T_STRING,期待 T_FUNCTION
    猜你喜欢
    • 2012-10-06
    • 1970-01-01
    • 2012-09-30
    • 1970-01-01
    • 1970-01-01
    • 2018-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多