Contao\ZipWriter::addFile PHP Method

addFile() public method

Add a file to the archive
public addFile ( string $strFile, string $strName = null )
$strFile string The file path
$strName string An optional file name
    public function addFile($strFile, $strName = null)
    {
        if (!file_exists(TL_ROOT . '/' . $strFile)) {
            throw new \Exception("File {$strFile} does not exist");
        }
        // Remove leading slashes (see #4502)
        if (strncmp($strName, '/', 1) === 0) {
            $strName = substr($strName, 1);
        }
        $this->addString(file_get_contents(TL_ROOT . '/' . $strFile), $strName ?: $strFile, filemtime(TL_ROOT . '/' . $strFile));
    }

Usage Example

Example #1
0
 /**
  * Add templates to the archive
  *
  * @param ZipWriter $objArchive
  * @param string    $strFolder
  */
 protected function addTemplatesToArchive(ZipWriter $objArchive, $strFolder)
 {
     // Strip the templates folder name
     $strFolder = preg_replace('@^templates/@', '', $strFolder);
     // Re-add the templates folder name
     if ($strFolder == '') {
         $strFolder = 'templates';
     } else {
         $strFolder = 'templates/' . $strFolder;
     }
     if (\Validator::isInsecurePath($strFolder)) {
         throw new \RuntimeException('Insecure path ' . $strFolder);
     }
     // Return if the folder does not exist
     if (!is_dir(TL_ROOT . '/' . $strFolder)) {
         return;
     }
     $arrAllowed = \StringUtil::trimsplit(',', strtolower(\Config::get('templateFiles')));
     array_push($arrAllowed, 'sql');
     // see #7048
     // Add all template files to the archive
     foreach (scan(TL_ROOT . '/' . $strFolder) as $strFile) {
         if (preg_match('/\\.(' . implode('|', $arrAllowed) . ')$/', $strFile) && strncmp($strFile, 'be_', 3) !== 0 && strncmp($strFile, 'nl_', 3) !== 0) {
             $objArchive->addFile($strFolder . '/' . $strFile);
         }
     }
 }
All Usage Examples Of Contao\ZipWriter::addFile