ZipArchive::CREATE is not creating a zip file and returning true

Shakti Sisodiya picture Shakti Sisodiya · May 16, 2017 · Viewed 7.8k times · Source

Hello i am just creating a zip file using php but the file not being created however it is return boolean true i dont know whats problem going on. before created what did i do

  1. i installed php zip in my Ubuntu using sudo apt-get install php-zip and after installation i restarted my apache server.

  2. create a zip.php file file and write this code

$zip = new ZipArchive; //var_dump($zip->open('test.zip',ZipArchive::CREATE)); $f = $zip->open('zip/myzip.zip',ZipArchive::CREATE); var_dump($f);

this code return true but file not being created

  1. no any file permission issue i already given it to my directory.

Answer

sangram singh picture sangram singh · May 16, 2017

Empty Zip can not be created, You have to select some files to create a zip file. Try below code

$zip = new ZipArchive();
$filename = "test.zip";

if ($zip->open($filename, ZipArchive::CREATE)!==TRUE) {
    exit("cannot open <$filename>\n");
}
$zip->addFile("a.jpg","b.jpg");
$zip->close();

replace 'a.jpg', 'b.jpg' from your files.