douyuan4357 2018-05-24 10:51
浏览 111
已采纳

TYPO3 isDir,getRealPath在Controller上不起作用

What i would like to do, is to zip my fileadmin and my extension folder with a zipAction on my controller. So i took a piece of code from here and i have this:

$fileAdminPath = 'fileadmin';
$archiveName = $importerSettings['fileadminArchiveName'];
$zip = new \ZipArchive;
$zip->open($archiveName, \ZipArchive::CREATE | \ZipArchive::OVERWRITE);
$files = new \RecursiveIteratorIterator (new \RecursiveDirectoryIterator($fileAdminPath), \RecursiveIteratorIterator::LEAVES_ONLY);

if (!$file->isDir()) {
   // Get real and relative path for current file
   $filePath = $file->getRealPath();
   $relativePath = substr($filePath, strlen($fileAdminPath) + 1);
   // Add current file to archive
   $zip->addFile($filePath, $relativePath);
}

// Zip archive will be created only after closing object
$zip->close();

Now, when the code reaches the isDir() part, i get the following error:

Call to a member function isDir() on null

If i remove it and let the code run again, the getRealPath() gets the same error. So i am guessing i missing a namespace or something like that, that stores these functions.

On a local php file (index.php) and MAMPP it works. On TYPO3, not.

By the way, i am not sure if the controller really reads the fileadmin path. If i got it wrong, please feel free to correct me.

Best regards

  • 写回答

1条回答 默认 最新

  • doubei8168 2018-05-24 11:32
    关注

    If you run your script inside an extension, this script is searching fileadmin within your extension directory.

    Try to use the following script:

    $fileAdminPath = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName('fileadmin');
    

    Edit:

    You should also walk the files in an foreach because you scan an directory recursiv:

    foreach ($files as $file) {
      if (!$file->isDir()) {
        // Get real and relative path for current file
        $filePath = $file->getRealPath();
        $relativePath = substr($filePath, strlen($fileAdminPath) + 1);
        // Add current file to archive
        $zip->addFile($filePath, $relativePath);
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 魔霸ROG7 pro,win11.息屏后会显示黑屏,如图,如何解决?(关键词-重新启动)
  • ¥15 有没有人知道这是哪里出了问题啊?要怎么改呀?
  • ¥200 C++表格文件处理-悬赏
  • ¥15 Windows Server2016本地登录失败
  • ¥15 复合卡卡号轨道写入芯片卡
  • ¥20 基于MATLAB的TDOA
  • ¥15 为啥输入字体突然变了
  • ¥20 已知坐标,换成MATLAB可以用的数据
  • ¥15 这个python五子棋代码的每一句意思是什么啊
  • ¥15 求一段隐藏代码,隐藏一些内容