1

我创建了一个模块,它具有:

<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->get("arquivo") 读取该参数时,我唯一得到的是文件名。我已经在目录上搜索了文件名,但一无所获。joomla 真的会上传那个文件吗?如果有,它放在哪里?提前致谢。

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

4

2 回答 2

1

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

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

你会发现一个数组“files”,它看起来像:(这里“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 用于将信息转储到服务器错误日志,您可以将其删除并用适当的异常替换它。

于 2013-08-12T10:40:26.173 回答
1

在 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
}
?>

更多可以在这个链接上找到如何使用文件系统包

于 2017-03-20T07:54:35.790 回答