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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?