0

new ZipArchive is not working in cakephp it showing

error :=Class 
'App\Controller\ZipArchive' not found

This is what I have tried so far:

$fl_name = array('1.pdf', '2.pdf');
$zipname = 'file.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
foreach ($fl_name as $file){
    $zip->addFile($file);
}
$zip->close();
header('Content-Type: application/zip');
header('Content-disposition: attachment; filename=' . $zipname);
header('Content-Length: ' . filesize($zipname));
readfile($zipname);
SCouto
  • 7,808
  • 5
  • 32
  • 49
  • That doesn't read like an actual error message would. Post the actual error message. – GetSet Jul 13 '20 at 04:01
  • Error in my cakephp is : Class 'App\Controller\ZipArchive' not found { $this->autoRender=false; $fl_name=array('1.pdf','2.pdf'); $zipname = 'file.zip'; $zip = new ZipArchive; **//Showing error here** $zip->open($zipname, ZipArchive::CREATE); foreach ($fl_name as $file) { $zip->addFile($file); } – Shripad Jalamkar Jul 13 '20 at 06:20
  • Does this answer your question? [Fatal error: Class 'ZipArchive' not found in](https://stackoverflow.com/questions/3872555/fatal-error-class-ziparchive-not-found-in) – Nick Jul 13 '20 at 09:23
  • This is a basic namespace issue, do some reading on that topic. You need to either add a `use` statement at the top of the file, or specify the full namespaced path to the class when you use `new`. – Greg Schmidt Jul 13 '20 at 15:52

1 Answers1

0

Example based on your names Make sure the directory is read/write!!!

<?php

$zip = new ZipArchive();
$filename = "./file.zip";

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

$zip->addFile($thisdir . "/1.pdf","/1.pdf");
$zip->addFile($thisdir . "/2.pdf","/2.pdf");
echo "numfiles: " . $zip->numFiles . "\n";
echo "status:" . $zip->status . "\n";
$zip->close();

?>

update to "Load Vendor Files"

I recommend using composer to add ZipArchive source to your code. Adding ZipArchive via composer should "just work" for autoloading

If autoload is not working for any vendor (composer installed) code then take a look at Loading Vendor Files

Artistan
  • 1,982
  • 22
  • 33
  • Thank you for your code..Actually i am trying this in cakephp it is showing Class 'App\Controller\ZipArchive' not found error.But this code is able to run in normal PHP file execution.Thank you – Shripad Jalamkar Jul 13 '20 at 06:14