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);
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?