PHPOffice / PHPWord

A pure PHP library for reading and writing word processing documents
https://phpoffice.github.io/PHPWord/
Other
7.17k stars 2.68k forks source link

Error with zip #965

Open carles9000 opened 7 years ago

carles9000 commented 7 years ago

Hi Masters,

Many times, if you repeat and repeat the program (for exemple with F5), the system gives an error message that can not close the temporary file.

The following message appears on the screen:

Fatal error: Uncaught exception 'PhpOffice\PhpWord\Exception\Exception' with message 'Could not close zip file C:\Users\songoku\AppData\Local\Temp\Php9D8B.tmp.' in C:\xampp\htdocs\z\doc\src\PhpWord\Shared\ZipArchive.php:163 Stack trace: #0 C:\xampp\htdocs\z\doc\src\PhpWord\TemplateProcessor.php(412): PhpOffice\PhpWord\Shared\ZipArchive->close() #1 C:\xampp\htdocs\z\doc\src\PhpWord\TemplateProcessor.php(430): PhpOffice\PhpWord\TemplateProcessor->save() #2 C:\xampp\htdocs\z\doc\S07c.php(63): PhpOffice\PhpWord\TemplateProcessor->saveAs('reporting/outpu...') #3 {main} thrown in C:\xampp\htdocs\z\doc\src\PhpWord\Shared\ZipArchive.php on line 163

Any idea how I can solve this situation ?

Thanks for you help. Carles

--- Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/40236087-error-with-zip?utm_campaign=plugin&utm_content=tracker%2F323108&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F323108&utm_medium=issues&utm_source=github).
carles9000 commented 7 years ago

Hi again,

If you change zip engine, it seems like you run many times and it doesn't break

I make this exchange in the configuration

Settings::setZipClass( 'PclZip' );

but file.docx is corrupt

Thanks. Carles.

carles9000 commented 7 years ago

More info...

I write in ZipArchive.php, method Close() (+-line 162) the next:

    public function close()
    {
        if (!$this->usePclzip) {
            if ($this->zip->close() === false) {
        echo '<br>ERROR: ' . $this->zip->getStatusString() ;
                throw new Exception("Could not close zip file {$this->filename}.");
            }
        }

        return true;
    }

Then execute and this is the output

ERROR: Renaming temporary file failed: Invalid argument Fatal error: Uncaught exception 'PhpOffice\PhpWord\Exception\Exception' with message 'Could not close zip file C:\Users\songoku\AppData\Local\Temp\PhpDF2F.tmp.' in C:\xampp\htdocs\z\doc\src\PhpWord\Shared\ZipArchive.php:164 Stack trace: #0 C:\xampp\htdocs\z\doc\src\PhpWord\TemplateProcessor.php(426): PhpOffice\PhpWord\Shared\ZipArchive->close() #1 C:\xampp\htdocs\z\doc\src\PhpWord\TemplateProcessor.php(448): PhpOffice\PhpWord\TemplateProcessor->save() #2 C:\xampp\htdocs\z\doc\S07d.php(44): PhpOffice\PhpWord\TemplateProcessor->saveAs('reporting/outpu...') #3 {main} thrown in C:\xampp\htdocs\z\doc\src\PhpWord\Shared\ZipArchive.php on line 164

The code is based in samples -> Sample_07_TemplateCloneRow.php

<?php
$vendorDirPath = 'vendor';

if (file_exists($vendorDirPath . '/autoload.php')) {
    require $vendorDirPath . '/autoload.php';
} else {
    throw new Exception(
        sprintf(
            'Could not find file \'%s\'. It is generated by Composer. Use \'install --prefer-source\' or \'update --prefer-source\' Composer commands to move forward.',
            $vendorDirPath . '/autoload.php'
        )
    );
}

use PhpOffice\PhpWord\Settings;

Settings::loadConfig('phpword.ini');

echo 'Tmp: ' . Settings::getTempDir();

$templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor('reporting/plantillas/S07d.docx');

// Table with a spanned cell
$templateProcessor->cloneRow('userId', 3);

$templateProcessor->setValue('userId#1', '1');
$templateProcessor->setValue('userFirstName#1', 'James');
$templateProcessor->setValue('userName#1', 'Taylor');
$templateProcessor->setValue('userPhone#1', '+1 428 889 773');

$templateProcessor->setValue('userId#2', '2');
$templateProcessor->setValue('userFirstName#2', 'Tesxto largo de test 1. Tesxto largo de test 2. Tesxto largo de test 3. Tesxto largo de test 4. ');
$templateProcessor->setValue('userName#2', 'Bell');
$templateProcessor->setValue('userPhone#2', '+1 428 889 774');

$templateProcessor->setValue('userId#3', '3');
$templateProcessor->setValue('userFirstName#3', 'Michael');
$templateProcessor->setValue('userName#3', 'Ray');
$templateProcessor->setValue('userPhone#3', '+1 428 889 775');
$templateProcessor->setValue('userPhone#3', '');

echo '<br>' . date('H:i:s'), ' Saving the result document... '  ;

$templateProcessor->saveAs('reporting/output/S07d.doc');
?>

Finally i tried with phpword.ini

; Default config file for PHPWord
; Copy this file into phpword.ini and use Settings::loadConfig to load

[General]

compatibility         = true
zipClass              = ZipArchive
pdfRendererName       = DomPDF
pdfRendererPath       = /vendor/dompdf/dompdf
;tempDir               = "/C:\PhpWordTemp"
tempDir               = /reporting/tmp
outputEscapingEnabled = false

[Font]

defaultFontName = Arial
defaultFontSize = 10

I read this issues but i can not find the correct solution...

Ideas, suggestions, help, please?

Carles.

florianp-dev commented 6 years ago

Hi there, I just had the problem.

Simply change rights to the folder was successfully.

vlyalko commented 5 years ago

@florianp-dev which folder rights needs to be changed for? /tmp?

chrisribe commented 5 years ago

Got this error when trying to open a .doc and not .docx (only open docx!)

almokhtarbr commented 3 years ago

Got this error when trying to open a .doc and not .docx (only open docx!)

any updates ?