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 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同