Search code examples
phpcodeigniterphp-ziparchive

ZipArchive extractTo throwing exception


I'm using ZipArchive within a Code Igniter framework PHP Application. It's working fine to extact however code igniter is throwing an error at the extractTo step but it doesn't crash the application and the application continues till the end but this error is still triggered and displayed by code igniter. The directory exists before extractTo and it extracts properly and keeps going but why is this error thrown?

        $zipVar = new ZipArchive;
        $res = $zipVar->open($input_zip); 
        if ($res === TRUE) 
        {                           
             $zipVar->extractTo($target_dir);
             $zipVar->close();                        
         } 

enter image description here


Solution

  • Try

    $input_zip = "" ; // 
    $target_dir = "" ; //
    
    
    if(!is_file($input_zip) || !is_readable($target_dir))
    {
        die("Can't Read Input");
    }
    
    if(!is_dir($target_dir) || !is_writable($target_dir))
    {
        die("Can't Write to Target");
    }
    
    $zip = new ZipArchive;
    $res = $zip->open($input_zip);
    if ($res === TRUE)
    {
        echo 'ok';
        $zip->extractTo($target_dir);
        $zip->close();
    }
    else {
        die("Failed");
    }