Search code examples
phpfilephp-7php-ziparchive

Creating Zip returns read error


I have these few lines that create an empty zip file that I will put things in.

$zip = new ZipArchive();
$generatedname = uniqid().'.zip';
$res = $zip->open('tmp/'.$generatedname, ZipArchive::CREATE);

$res is 5, which corresponds with a read error, and when I go to add files I get an error that the zip is not initialized. This code works on my local machine but not on my iis server, so it's some kind of configuration error?

I can read and write files with fopen and fwrite, so I don't think is has to do with rw permissions, so I'm kinda out of troubleshooting ideas.


Solution

  • Ok, I actually figured this one out. The issue is actually with where the zip is being created. I had it set to /tmp in my working directory since I was going to have the file downloaded and then immediately deleted.

    So I found this article that talks about using a directory the will always be writable by php, so creating my zip file in the system temp directory seems to have done the trick. Heres the updated code:

    chdir( sys_get_temp_dir() );
    
    $zip = new ZipArchive();
    $generatedname = uniqid().'.zip';
    $zip->open($generatedname, ZipArchive::CREATE);