Joomla在模块字段上使用type =“file”时放置文件?

时间:2013-08-11 13:47:09

标签: joomla upload module field

我创建了一个模块:

<field
                    name="arquivo"
                    type="file"
                    label="Arquivo de Aniversariantes"
                    description="Arquivo na extensão .CSV com as colunas: NOME, DIA, MES"
                    size="200" 
                    required="required"
                    accept="text/comma-separated-values, text/csv, application/csv, application/excel, application/vnd.ms-excel, application/vnd.msexcel, text/anytext"
                />

当我尝试通过$ params-&gt; get(“arquivo”)读取该参数时,我唯一得到的是文件名。我已经在目录上搜索了文件名并没有找到任何内容。 joomla真的上传了那个文件吗?如果确实如此,它放在哪里? 提前谢谢。

修改 我刚刚在Joomla的管理面板上查看了所选模块上的 form 标签,它缺少文件的enctype,也许这就是问题所在?如果是的话,我怎么把它放在那里?

2 个答案:

答案 0 :(得分:1)

将多部分编码添加到xml定义中的表单后:

<?xml version="1.0" encoding="utf-8"?>
<form enctype="multipart/form-data">
    <fieldset>

你会发现一个数组“文件”看起来像:(这里“pdf”是字段名称)

 'name' => array ( 'pdf' => '', ), 
 'type' => array ( 'pdf' => '', ), 
 'tmp_name' => array ( 'pdf' => '', ), 
 'error' => array ( 'pdf' => 4, ), 
 'size' => array ( 'pdf' => 0, ), )

空时,

 'name' => array ( 'pdf' => '8.jpg', ), 
 'type' => array ( 'pdf' => 'image/jpeg', ), 
 'tmp_name' => array ( 'pdf' => '/tmp/phpk1fDmB', ), 
 'error' => array ( 'pdf' => 0, ), 
 'size' => array ( 'pdf' => 26975, ),

满员。临时文件夹是php tmp文件夹,而不是Joomla的文件夹。您可以根据需要调整此功能:

private function getFile($key,$destinationFolder) {
    /**
     *  now let's process uploads: the array files contains a key "$key" which is the key name.
     *  we need to copy the files uploaded
     *  (if any are there and if they match the field filter = pdf)
     *  and set the data->pdf to its new path.
     * */
             $file = JRequest::getVar('jform', array(), 'files', 'array');
             if ($file['error'][$key]!="0") {
                error_log('no files uploaded, exiting now');
                return "";
             }

             //error_log('OFFER FOUND FILES '.var_export($file,true));
             $tempName = $file['tmp_name'][$key];
             $tempFullPath = ini_get('upload_tmp_dir').$tempName;
             $type = $file['type'][$key];
             $name = $file['name'][$key];
             //error_log('DATA FOUND: '. "temp: $tempName , type: $type, name: $name");
             if (file_exists($tempFullPath))
             {
                if (mkdir(JPATH_SITE.$destinationFolder,0755,true)) {
                if (copy($source = $tempFullPath, $dest = JPATH_SITE.$destinationFolder."/".$name)) {
                    return $destinationFolder."/".$name;
                } else
                {
                    error_log('could not copy '. "$source to $dest");

                }
                } else {
                    error_log('could not create folder '. JPATH_SITE.$destinationFolder);
                }
                return "";
             } else {
                error_log('FILE NOT FOUND: '. $tempFullPath);
             }

error_log用于将信息转储到服务器错误日志,您可以删除它并将其替换为相应的例外。

答案 1 :(得分:0)

在Joomla中有很好的方法,这是来自Joomla文档的代码片段

假设这是样本表格

/*Sample Form either form form.xml file or from view file */
<form name="upload" method="post" enctype="multipart/form-data">
<input type="file" name="file_upload" />
<input type="submit" />
</form>


/*File handling code over here*/
    <?php
/*
* 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;

//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))
   {
      //Redirect to a page of your choice
   } 
   else
   {
      //Redirect and throw an error message
   }
}
else
{
   //Redirect and notify user file is not right extension
}
?>

可在此链接How to use the filesystem package

上找到更多内容