PHPOffice/PHPWord · error · Exception

Could not open ' ' for writing.

Error message

Could not open '{$filename}' for writing.

What it means

getZipArchive could not create/open the package file with ZipArchive::OVERWRITE nor ZipArchive::CREATE after unlinking any existing file, meaning the target path is unwritable (permissions, read-only volume, invalid path) and the save cannot build its ZIP container.

Solutions

  1. Verify the output directory exists and is writable by the PHP process.
  2. Check disk space and that the path is not on a read-only filesystem.
  3. Ensure the ZipArchive PHP extension is installed and functioning.
  4. Save to a temporary writable path and move the archive into place afterward.
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at src/PhpWord/Writer/AbstractWriter.php:285 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of PHPOffice/PHPWord@aef95c0415 (2026-09-14). Data as JSON: /api/errors/bfe97c446bceb47f. Report an issue: GitHub.

Appendix: source

Thrown at src/PhpWord/Writer/AbstractWriter.php:285

     * @param string $filename
     *
     * @return ZipArchive
     */
    protected function getZipArchive($filename)
    {
        // Remove any existing file
        if (file_exists($filename)) {
            unlink($filename);
        }

        // Try opening the ZIP file
        $zip = new ZipArchive();

        // @codeCoverageIgnoreStart
        // Can't find any test case. Uncomment when found.
        if ($zip->open($filename, ZipArchive::OVERWRITE) !== true) {
            if ($zip->open($filename, ZipArchive::CREATE) !== true) {
                throw new \Exception("Could not open '{$filename}' for writing.");
            }
        }
        // @codeCoverageIgnoreEnd

        return $zip;
    }

    /**
     * Open file for writing.
     *
     * @since 0.11.0
     *
     * @param string $filename
     *
     * @return resource
     */
    protected function openFile($filename)
    {

View on GitHub (pinned to aef95c0415)