Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ZipArchive::close(): Renaming temporary file failed: Permission denied{ "message": "Invalid or uninitialized Zip object" #2613

Closed
ozilion opened this issue May 27, 2024 · 4 comments
Milestone

Comments

@ozilion
Copy link
Contributor

ozilion commented May 27, 2024

Hi all,

I am trying integrating old PHP project into Laravel v10 and using PHPWord v1.2.

I have PHPWord v0.17 in old project and using TemplateProcessor without any problem and creating all my Word files (5 to 20 files depending criteria) successfully, but while integrating to Laravel + PHPWord v1.2 I am getting error "ZipArchive::close(): Renaming temporary file failed: Permission denied{ "message": "Invalid or uninitialized Zip object"," for some files and it fails to create files randomly and sometimes it works correctly without any issue. I know the template file is ok and can be opened without any issue and works in old project.

Here is the function i use for teh templating, I call it for each file i need to create with an order.

public function setDosyasiOlustur($filem, $newFilem, $pot, $setpath, $canzipped = false, $asama = "")
  {
    try {
      $pati = $setpath;
      $a1sahaliste9 = array("01", "02", "03", "05", "09", "11", "12", "13", "14", "15", "18", "19", "20", "21", "22", "24", "25", "26", "28", "33", "37", "38");
      $a1sahaliste14 = array("01", "02", "03", "04", "05", "07", "09", "10", "11", "12", "13", "15", "16", "17", "20", "21", "24", "25", "26", "28", "29", "35", "36", "38", "39");
      $a1sahaliste45 = array("01", "02", "03", "04", "05", "06", "07", "09", "10", "11", "12", "13", "17", "24", "25", "26", "27", "28", "29", "30", "31", "34", "35", "36", "37", "38", "39");

      $pot["iso900115varyok"] = isset($pot["iso900115varyok"]) ? $pot["iso900115varyok"] : 0;
      $pot["iso1400115varyok"] = isset($pot["iso1400115varyok"]) ? $pot["iso1400115varyok"] : 0;
      $pot["iso2200018varyok"] = isset($pot["iso2200018varyok"]) ? $pot["iso2200018varyok"] : 0;
      $pot["iso450001varyok"] = isset($pot["iso450001varyok"]) ? $pot["iso450001varyok"] : 0;
      $pot["iso50001varyok"] = isset($pot["iso50001varyok"]) ? $pot["iso50001varyok"] : 0;
      $pot["iso27001varyok"] = isset($pot["iso27001varyok"]) ? $pot["iso27001varyok"] : 0;
      $pot["helalvaryok"] = isset($pot["helalvaryok"]) ? $pot["helalvaryok"] : 0;
      $pot["oicsmiik6varyok"] = isset($pot["oicsmiik6varyok"]) ? $pot["oicsmiik6varyok"] : 0;
      $pot["oicsmiik9varyok"] = isset($pot["oicsmiik9varyok"]) ? $pot["oicsmiik9varyok"] : 0;
      $pot["oicsmiik171varyok"] = isset($pot["oicsmiik171varyok"]) ? $pot["oicsmiik171varyok"] : 0;
      $pot["oicsmiik23varyok"] = isset($pot["oicsmiik23varyok"]) ? $pot["oicsmiik23varyok"] : 0;
      $pot["oicsmiik24varyok"] = isset($pot["oicsmiik24varyok"]) ? $pot["oicsmiik24varyok"] : 0;
      $pot["entegreysvarmi"] = isset($pot["indartentvarmi"]) ? $pot["indartentvarmi"] : 0;

      if ($this->doc == "") {
        Settings::loadConfig();
        Settings::setTempDir(public_path() . "/temp");
        Settings::setOutputEscapingEnabled(true);
        Settings::setCompatibility(true);

        $this->doc = new TemplateProcessor($filem);
        $this->doc->setMacroOpeningChars("æ");
        $this->doc->setMacroClosingChars("æ");
        $this->doc->setMacroChars("æ", "æ");
      }

//      echo Settings::getTempDir();
      if ($pot["iso900115varyok"] == 0) {
        $this->doc->deleteBlock('deletekys');
      }
      if ($pot["iso1400115varyok"] == 0) {
        $this->doc->deleteBlock('deletecys');
      }
      if ($pot["iso2200018varyok"] == 0) {
        $this->doc->deleteBlock('deleteggys18');
      }
      if ($pot["helalvaryok"] == 0) {
        $this->doc->deleteBlock('deleteoicsmiic');
      }
      if ($pot["oicsmiik6varyok"] == 0) {
        $this->doc->deleteBlock('deleteoicsmiic6');
      }
      if ($pot["oicsmiik9varyok"] == 0) {
        $this->doc->deleteBlock('deleteoicsmiic9');
      }
      if ($pot["oicsmiik171varyok"] == 0) {
        $this->doc->deleteBlock('deleteoicsmiic171');
      }
      if ($pot["oicsmiik23varyok"] == 0) {
        $this->doc->deleteBlock('deleteoicsmiic23');
      }
      if ($pot["oicsmiik24varyok"] == 0) {
        $this->doc->deleteBlock('deleteoicsmiic24');
      }
      if ($pot["iso4500118varyok"] == 0) {
        $this->doc->deleteBlock('deleteo4500118');
      }
      if ($pot["iso5000118varyok"] == 0) {
        $this->doc->deleteBlock('deleteo5000118');
      }
      if ($pot["iso27001varyok"] == 0) {
        $this->doc->deleteBlock('delete2700122');
      }

      if (isset($pot["iso900115varyok"]) && $pot["iso900115varyok"] == 1) $this->doc->setCheckboxMS('w:name="iso900115varyok"', true);
      if (isset($pot["iso1400115varyok"]) && $pot["iso1400115varyok"] == 1) $this->doc->setCheckboxMS('w:name="iso1400115varyok"', true);
      if (isset($pot["iso4500118varyok"]) && $pot["iso4500118varyok"] == 1) $this->doc->setCheckboxMS('w:name="iso4500118varyok"', true);
      if (isset($pot["iso5000118varyok"]) && $pot["iso5000118varyok"] == 1) $this->doc->setCheckboxMS('w:name="iso5000118varyok"', true);
      if (isset($pot["iso2200018varyok"]) && $pot["iso2200018varyok"] == 1) $this->doc->setCheckboxMS('w:name="iso2200018varyok"', true);
      if (isset($pot["iso2200018varyok"]) && $pot["iso2200018varyok"] == 1) $this->doc->setCheckboxMS('w:name="abirsaha"', true);
      if (isset($pot["iso50001varyok"]) && $pot["iso50001varyok"] == 1) $this->doc->setCheckboxMS('w:name="abirsaha"', true);
      if (isset($pot["helalvaryok"]) && $pot["helalvaryok"] == 1) $this->doc->setCheckboxMS('w:name="helalvaryok"', true);
      if (isset($pot["oicsmiik6varyok"]) && $pot["oicsmiik6varyok"] == 1) $this->doc->setCheckboxMS('w:name="oicsmiik6varyok"', true);
      if (isset($pot["oicsmiik9varyok"]) && $pot["oicsmiik9varyok"] == 1) $this->doc->setCheckboxMS('w:name="oicsmiik9varyok"', true);
      if (isset($pot["oicsmiik171varyok"]) && $pot["oicsmiik171varyok"] == 1) $this->doc->setCheckboxMS('w:name="oicsmiik171varyok"', true);
      if (isset($pot["oicsmiik23varyok"]) && $pot["oicsmiik23varyok"] == 1) $this->doc->setCheckboxMS('w:name="oicsmiik23varyok"', true);
      if (isset($pot["oicsmiik23varyok"]) && $pot["oicsmiik23varyok"] == 1) $this->doc->setCheckboxMS('w:name="oicsmiik24varyok"', true);
      if (isset($pot["digersistemlerneler"]) && $pot["digersistemlerneler"] != "") $this->doc->setCheckboxMS('w:name="digervaryok"', true);
      if (isset($pot["entegreysvarmi"]) && $pot["entegreysvarmi"] == 1) $this->doc->setCheckboxMS('w:name="entegreysvarmi"', true);
      if (isset($pot["yonetimsistemsertifikasi"]) && $pot["yonetimsistemsertifikasi"] == "1") $this->doc->setCheckboxMS('w:name="yonetimsistemievet"', true);
      if (isset($pot["yonetimsistemsertifikasi"]) && $pot["yonetimsistemsertifikasi"] == "0") $this->doc->setCheckboxMS('w:name="yonetimsistemihayir"', true);
      if (isset($pot["tumvardayni"]) && $pot["tumvardayni"] == "EVET") $this->doc->setCheckboxMS('w:name="tumvardaynievet"', true);
      if (isset($pot["tumvardayni"]) && $pot["tumvardayni"] == "HAYIR") $this->doc->setCheckboxMS('w:name="tumvardaynihayir"', true);
      if (isset($pot["belgedurum"]) && $pot["belgedurum"] == "iptal") $this->doc->setCheckboxMS('w:name="belgeiptal"', true);
      if (isset($pot["belgedurum"]) && $pot["belgedurum"] == "aski") $this->doc->setCheckboxMS('w:name="belgeaski"', true);
      if (isset($pot["belgedurum"]) && $pot["belgedurum"] == "askiindir") $this->doc->setCheckboxMS('w:name="belgeaskiindir"', true);
      if (isset($pot["belgedurum"]) && $pot["belgedurum"] == "devam") $this->doc->setCheckboxMS('w:name="belgeok"', true);
      if (isset($pot["belgedurum"]) && $pot["belgedurum"] == "devam") $this->doc->setCheckboxMS('w:name="belgeaski"', false);
      if (isset($pot["belgedurum"]) && $pot["belgedurum"] == "devam") $this->doc->setCheckboxMS('w:name="belgeaskiindir"', false);
      if (isset($pot["subeturu1"]) && $pot["subeturu1"] == "1") $this->doc->setCheckboxMS('w:name="subeturu1"', true);
      if (isset($pot["subeturu1"]) && $pot["subeturu1"] == "2") $this->doc->setCheckboxMS('w:name="subeturu1"', true);
      if (isset($pot["subeturu2"]) && $pot["subeturu2"] == "1") $this->doc->setCheckboxMS('w:name="subeturu2"', true);
      if (isset($pot["subeturu2"]) && $pot["subeturu2"] == "2") $this->doc->setCheckboxMS('w:name="subeturu2"', true);
      if (isset($pot["subeturu3"]) && $pot["subeturu3"] == "1") $this->doc->setCheckboxMS('w:name="subeturu3"', true);
      if (isset($pot["subeturu3"]) && $pot["subeturu3"] == "2") $this->doc->setCheckboxMS('w:name="subeturu3"', true);
      if (isset($pot["subeturu4"]) && $pot["subeturu4"] == "1") $this->doc->setCheckboxMS('w:name="subeturu4"', true);
      if (isset($pot["subeturu4"]) && $pot["subeturu4"] == "2") $this->doc->setCheckboxMS('w:name="subeturu4"', true);
      //ENTEGRE SİSTEM SEÇENEKLERİ
      if (isset($pot["ygg"]) && $pot["ygg"] == "12.5") $this->doc->setCheckboxMS('w:name="ygg"', true);
      if (isset($pot["icdenetim"]) && $pot["icdenetim"] == "12.5") $this->doc->setCheckboxMS('w:name="icdenetim"', true);
      if (isset($pot["entegredokumantasyon"]) && $pot["entegredokumantasyon"] == "12.5") $this->doc->setCheckboxMS('w:name="entegredokumantasyon"', true);
      if (isset($pot["duzelticifaaliyet"]) && $pot["duzelticifaaliyet"] == "12.5") $this->doc->setCheckboxMS('w:name="duzelticifaaliyet"', true);
      if (isset($pot["yondessor"]) && $pot["yondessor"] == "12.5") $this->doc->setCheckboxMS('w:name="yondessor"', true);
      if (isset($pot["prosesentegre"]) && $pot["prosesentegre"] == "12.5") $this->doc->setCheckboxMS('w:name="prosesentegre"', true);
      if (isset($pot["politikahedefler"]) && $pot["politikahedefler"] == "12.5") $this->doc->setCheckboxMS('w:name="politikahedefler"', true);
      if (isset($pot["riskyonetimyaklasimi"]) && $pot["riskyonetimyaklasimi"] == "12.5") $this->doc->setCheckboxMS('w:name="riskyonetimyaklasimi"', true);
      // abirsaha  // abirofis

      $this->doc->setCheckboxMS('w:name="abirsaha"', false);
      $this->doc->setCheckboxMS('w:name="abirofis"', true);

      foreach ($a1sahaliste9 as $key => $value) {
        if ($pot["iso900115varyok"] == 1 && intval($pot["eakodu"]) == intval($value)) $this->doc->setCheckboxMS('w:name="abirsaha"', true);
        if ($pot["iso900115varyok"] == 1 && intval($pot["eakodu"]) == intval($value)) $this->doc->setCheckboxMS('w:name="abirofis"', false);
      }
      foreach ($a1sahaliste14 as $key => $value) {
        if ($pot["iso1400115varyok"] == 1 && intval($pot["eakodu"]) == intval($value)) $this->doc->setCheckboxMS('w:name="abirsaha"', true);
        if ($pot["iso1400115varyok"] == 1 && intval($pot["eakodu"]) == intval($value)) $this->doc->setCheckboxMS('w:name="abirofis"', false);
      }
      foreach ($a1sahaliste45 as $key => $value) {
        if ($pot["iso4500118varyok"] == 1 && intval($pot["eakodu"]) == intval($value)) $this->doc->setCheckboxMS('w:name="abirsaha"', true);
        if ($pot["iso4500118varyok"] == 1 && intval($pot["eakodu"]) == intval($value)) $this->doc->setCheckboxMS('w:name="abirofis"', false);
      }

      if ($pot["iso2200018varyok"] == 1 || $pot["helalvaryok"] == 1 || $pot["oicsmiik6varyok"] == 1 || $pot["oicsmiik9varyok"] == 1 || $pot["oicsmiik171varyok"] == 1 || $pot["oicsmiik23varyok"] == 1 || $pot["oicsmiik24varyok"] == 1) {
        $this->doc->setCheckboxMS('w:name="abirsaha"', true);
        $this->doc->setCheckboxMS('w:name="abirofis"', false);
      }
//            var_dump($pot);
      foreach ($pot as $name => $value) {
        if ($name === "id") continue;
//                echo $name." = ". $value."<br>";
        $this->doc->setValue(strtolower($name), $value);
//                echo $name . " = " . $value . "<br>";
      }

      if (!file_exists($pati)) {
        if (!self::mkdirr($pati)) {
          self::msgError($pati . " dizini oluşturulamadı...", false);
        }
      }

//        echo $this->clearLocale($newFilem);
      $fname = $pati . '/' . iconv("UTF-8", "ISO-8859-9", $newFilem);
      if (file_exists($fname)) {
        unlink($fname);
      }
      $this->doc->saveAs($fname);
      $this->doc = "";

//      rename($fname, "{$fname}");

      $fname1 = "../setler/" . $pot["planno"] . "/" . $asama . "/" . iconv("ISO-8859-9", "UTF-8", $newFilem);
      echo '<a href="' . route("downloadWordFile", ['pno'=>$pot["planno"], 'asama'=>$asama, 'dosya'=>iconv("ISO-8859-9", "UTF-8", $newFilem)]) . '" class="col-sm-12 btn btn-success btn-sm" target="_blank">' . iconv("ISO-8859-9", "UTF-8", $newFilem) . '</a><br>';
      if ($canzipped) $this->canzip = $canzipped;
    } catch (Exception $e) {
      echo $e->getMessage();
    }
  }

Any help appreciated to solve this.

Regards,

@bunglegrind
Copy link

bunglegrind commented Jul 8, 2024

There are other issues already opened concerning this problem...I have experienced it too...but I've just found the culprit.
There's a bug in https://github.com/PHPOffice/PHPWord/blob/master/src/PhpWord/TemplateProcessor.php

In short, when saving the the file, the zip::close method is invoked two times:

  • the first one in templateprocessor::save

if (false === $this->zipClass->close()) {

  • the second one (and this one generates the warning) in templateprocessor::__deconstruct

$this->zipClass->close();

The bug was introduced in commit 21f71c9

However, I've no idea if commenting the line is the right patch (probably not, even if it's fine for my use case)

@oleibman I see you're the author of that commit, any opinions?

P.S.
I'm using PHP 7.4 (not sure if a Warning is generated or an Exception is thrown with PHP 8...)

@oleibman
Copy link
Contributor

oleibman commented Jul 8, 2024

@bunglegrind PR #2554, opened in January, will solve the problem if it is merged.

@bunglegrind
Copy link

bunglegrind commented Jul 9, 2024

@bunglegrind PR #2554, opened in January, will solve the problem if it is merged.

uh...thanks...there are even more issues. I'll take a look at the PR and possibly comment there.

Is this repository lacking moderators?

@oleibman
Copy link
Contributor

PR #2554 is now merged. You can close this issue, unless, of course, it doesn't fix your problem after all.

@Progi1984 Progi1984 added this to the 1.3.0 milestone Aug 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

4 participants