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 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序,怎么查看客户esp32板子上程序及烧录地址
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址