0
votes

I'm trying to create a zip file with a flat folder/file structure with PHP ZipArchive that contains two PDF files. So basically both files should be in the root of the zip file. I use the code below to create the zipfile.

$zip = new ZipArchive();

$zip_filename = "zipfile.zip";

if ($zip->open($zip_filename, ZipArchive::CREATE)!==TRUE) {
    exit("cannot open <$filename>\n");
}

$zip->addFile("cover_file.pdf", "cover_file.pdf");
$zip->addFile("inlay_file.pdf", "cover_file.pdf");              
$zip->close();

Whatever I do, the files are always placed inside a folder with the samen name as the zipfile.

Is there a way to save the files to the root of the zip file?

Thanks in advance!

1
What version of php are you using? - Rafael

1 Answers

0
votes

Worked Fine for Me

Index.php

<?php

$zip = new ZipArchive();

if (!$zip->open('zipfile.zip', ZipArchive::CREATE))
    exit("cannot open <$filename>\n");

$zip->addFromString('test.txt', 'tf is this?');
$zip->close();

Start Webserver

$ php -S localhost:8000

Output is zipfile.zip with test.txt at the root.

|--testProject
|----index.php
|----zipfile.zip
|--zipfile.zip
|----test.txt

Example w/ Multiple Files

<?php

$zip = new ZipArchive();

if (!$zip->open('zipfile.zip', ZipArchive::CREATE))
    exit("cannot open <$filename>\n");

for ($i=0; $i<7; $i++)
    $zip->addFromString("test$i.txt", 'tf is this?');

$zip->close();

Outputs

|--zipfile.zip
|----text0.txt
|----text1.txt
|----text2.txt
|----text3.txt
|----text4.txt
|----text5.txt
|----text6.txt

Notes: This extension has no configuration directives defined in php.ini

Tested on PHP 5.5.15