TYPO3 扩展模型与 f:form.upload

问题描述

对于自己的扩展,我想使用 viewhelper f:form.upload 要检索上传参数,我必须将此视图助手的属性定义为数组。这些可以很好地获取名称、类型、tmp_name、错误和大小参数。 在域模型中,我读取参数并保存上传的文件并将文件名存储在数据库中。这很好用!这里是变量 iFile 的域模型部分:

/**
 * iFile
 * 
 * @var array
 * @TYPO3\CMS\Extbase\Annotation\ORM\Cascade("remove")
 */
protected $iFile = [];

/**
 * Returns the iFile
 * 
 * @return string iFile
 */
public function getIFile()
{
    $temp = [];
$temp['image_name'] = $this->iFile;
    error_log("Get_iFile: " . var_export($temp,true),0);
    error_log("Get_iFile0: " .  var_export($this,0);
    return $temp;
}

/**
 * Sets the iFile
 * 
 * @param string $iFile
 * @return void
 */
public function setIFile($iFile)
{
    if($iFile['tmp_name'] != ""){
        $docRoot = $_SERVER['DOCUMENT_ROOT'].'\\' ;
        if (!is_dir($docRoot)) {
            mkdir($docRoot,07777,true);
        }
        $date = new \DateTime();
        $fName = 'fileadmin\\Radio\\crNews\\' . $date->format('Ymd') . $iFile['name'];
        if(copy($iFile['tmp_name'],$docRoot . $fName) == true);
            unlink($iFile['tmp_name']);
    }
    error_log("Set _FILES: " . var_export($iFile,0);
    $this->iFile = $fName;
}

但是如果我尝试从数据库中读取它,我不会从数据库中取回文件名!只有一个空数组。

   'iFile' => 
   array (
   ),

问题似乎是,由于将 iFile 定义为数组。 我该如何解决问题?

有没有可能,将一个新变量作为数组添加到模型中,而不是将其存储在数据库中?

解决方法

所以我找到了一个解决方法。我只将 iFile 定义为一个字符串来读取文件名并将其保存在数据库中,并使用 $_FILES 后变量来检索 $_FILES 的名称和 tmp_name。 在 Typo3 中,访问 $_FILES 参数并不是那么容易。所以我推荐使用phpinstruction error_log("Create _FILES: " . var_export($_FILES,true),0);来找出数组的结构。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...