4

How can you create a ZIP file using the fopen() wrapper? This is obviously not the way:

<?php

if( class_exists('ZipArchive') ){
    echo 'Class ZipArchive exists, generating file...' . PHP_EOL;

    $fp = fopen('zip://' . dirname(__FILE__) . '/test.zip', 'w');
    if($fp){
        fwrite($fp, 'Lorem ipsum dolor sit amet, consectetur adipisicing elit.');
        fclose($fp);
        echo 'Done' . PHP_EOL;
    }else{
        echo 'Could not open file' . PHP_EOL;
    }
}else{
    echo 'Class Zip is not available' . PHP_EOL;
}

... because all I get is:

Class ZipArchive exists, generating file...

Warning: fopen(zip://C:\tmp/test.zip) [http://es.php.net/function.fopen]: failed to open stream: operation failed in C:\tmp\test.php on line 6
Could not open file
Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • Is there a reason, why you dont use the `ZipArchive` class? Does `c:\tmp` exists and it is writeable for the user? – KingCrunch Apr 11 '11 at 11:50
  • @KingCrunch - I'm trying to feed $fp into some third-party code that accepts a file pointer as parameter. Code works as expected if I remove the `zip://` part from the fopen() call so permissions are okay. – Álvaro González Apr 11 '11 at 11:58
  • http://www.php.net/manual/en/function.ziparchive-getstream.php should be installedfrom pecl – Fivell Apr 11 '11 at 12:03
  • @Fivell - «Get a file handler to the entry defined by its name. For now it only supports read operations.» :-? – Álvaro González Apr 11 '11 at 12:07

1 Answers1

2

I finally assumed that the claim that zip: wrapper supports writing was a documentation error and reported it as such. The bug report was accepted and fixed.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360