Search code examples
phpzipextractphp-ziparchive

how to unzip .zip file in codeigniter 4 (PHP)


I am trying to extract zip file in codeigniter 4... I have the code below

    $file_name =  session()->get('file_name');
        
        // Zip file name
    $filename = "/public/$file_name";

    $zip = new \ZipArchive;
    
    $res = $zip->open($filename);
    
    if ($res === true) {

 // Unzip path
        $path = "/public";

        // Extract file
        $zip->extractTo($path);
        $zip->close();

        return 'Site Updated Successfully';
        
    } else {
        
        return "failed to update $filename!";
        
    }
        }

but I get this result: return "failed to update $filename!";

Please can someone help me fix the issue with the code.


Solution

  • This has nothing specifically to do with Codeigniter. It's just using PHP and ZipArchive. The code looks OK - be sure the file is really in the /public/ directory and that it is a zip archive. The PHP site has examples that are like $zip = new ZipArchive; (without the \ char); maybe try that.