Search code examples
phpphp-ziparchive

CakePHP ZipArchive undedifined method


I want to type

class ZipController extends AppController {

$folder_path = 'c:/Testaufgabe';
$zip_name = 'c:/my_zipped_file.zip'; // to simiou kai to onoma tou arxeiou pou tha apothikeuti                

        public function upload(){
        $za = new ZipArchive();
        $res = $za->open($zip_name, ZipArchive::CREATE);

        if($res === TRUE){  
            //$za->addFile($folder_path , basename($folder_path ));   // edo gia deuteri parametro theloume to onoma mono tou arxeiou
            $za->addDir($folder_path, basename($folder_path));
            $za->close();
        } 
        else  { 
            echo 'Could not create a zip archive';
        }   }



public function addDir($path, $root){
//   $za->addEmptyDir($root);
    //echo 'ok';
   // $this->addDirDo($path, $root, $za);
}}

But when I call $za->addDir($folder_path, basename($folder_path));

it says that Call to undefined method ZipArchive::addDir()

I would like to have some help please

I use CAKEPHP V3


Solution

  • You cannot call a method on a ZipArchive object that does not exist. You created the method in the controller, not in the ZipArchive class... What about calling the method of the controller instead?

        if($res === TRUE){  
            $this->addDir($za, $folder_path, basename($folder_path));
            $za->close();
        }
    
    public function addDir($za, $path, $root){
       $za->addEmptyDir($root);
       echo 'ok';
       $this->addDirDo($path, $root, $za);
    }