Joomla 3模块中的文件上传

时间:2014-12-23 10:02:19

标签: file joomla module upload

我想在joomla 3后端模块中上传文件。

我提到https://docs.joomla.org/How_to_use_the_filesystem_package但没有出现文件上传按钮。也许我弄错了或把它放在错误的文件中?

任何人都可以提供帮助? :d

谢谢。

下面是.xml文件和语言标签之间的代码。

<params>
    <param name="dir" type="text" label="Directory" description="Directory Upload" default="upload"/>
    <param name="type" type="list" default="*" label="Select a file type" description="File type">
         <option value="*">Any File </option>
         <option value="image/png">PNG</option>
         <option value="image/gif">GIF</option>
         <option value="image/jpeg">JPEG</option>
         <option value="application/zip">ZIP</option>
         <option value="application/x-gzip">TAR.GZ</option>
         <option value="text/html">HTML</option>
         <option value="text/plain">TXT</option>
         <option value="application/pdf">PDF</option>
         <option value="application/msword">DOC</option>
       </param>          
    <param name="user_names" type="text" label="User Names (optional)" description="Names users (optionas)" default=""/>    
</params>

在定义(&#39; _JEXEC&#39;)之后的mymodule.php中,或者死亡(&#39;限制访问&#39;);&#39;线。

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

$max = ini_get('upload_max_filesize');
$module_dir = $params->get( 'dir' );
$file_type = $params->get( 'type' );

$user_names = $params->get( 'user_names' );
$msg = '';

function fileUpload($max, $module_dir, $file_type, $msg){
//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');
    }

    $msg = "<script>alert('". $msg ."');</script>";
}
return $msg;
}

$user =& JFactory::getUser();
$username = $user->get('username');

$acc = 0;
$session =& JFactory::getSession(); 

if(isset($user_names)) {
$more = strpos($user_names, ',',0);
if($more >0){
    $user_names = explode(',',$user_names);
    foreach($user_names as $un){
        if($un == $username) {
            $session->set($acc, 1); 
        }else{
            $session->set($acc, 0); 
        }
    }
}else{
    if ($user_names == $username) $session->set($acc, 1); 
}
}else{
if(isset($username)) $session->set($acc, 1); 
}

if($session->get($acc) == 1){
?>

<form name="imgform" method="post" action="" enctype="multipart/form-data"     onSubmit="if(file_upload.value=='') {alert('Choose a file!');return false;}">
<?php echo JText::_('CHOOSE_FILE'); ?> <input type="file" name="file_upload" size="10" />
<input name="submit" type="submit" value="Upload" />
</form>

<?php
print fileUpload($max, $module_dir, $file_type, $msg);
}

// Adapted from http://docs.joomla.org/How_to_use_the_filesystem_package
?>

1 个答案:

答案 0 :(得分:0)

好的2件事是错的。

首先,您使用<params>这是参数字段的旧方法。您需要使用以下文档中显示的字段:

https://docs.joomla.org/J3.x:Creating_a_simple_module/Adding_Form_Fields

其次,您的mymodule.php文件用于模块的前端,除了抓取参数等数据外,不会与后端有任何关系。

您可以使用创建mediacustom form field类型字段。

希望这有帮助