Search code examples
phpwordpressgoogle-drive-apiphp-ziparchive

Zip extract returns error code 19 but the file I am extracting is a zip file


I am trying to extract the zip file in PHP the path I am providing plus the filename each and everything is 100% correct but still when I run the script it returns Err_Code 19 . File is not a zip Archive. The main module is that it downloads the zip file from google drive and then extract it and restores the whole site when I run the same module and download file from FTP or Dropbox or S3 it worked perfectly fine but when downloaded it from drive it starts giving me this error Here is the code I am trying.

$extractTo = wp_upload_dir()['basedir'];

        $zip = new ZipArchive;
        $res = $zip->open($zipFile);
        if (is_resource($res)) {
            echo 'HERE'; exit;
            $zip->extractTo($extractTo);
            $zip->close();

            $dirs = array_filter(glob($extractTo . '/*'), 'is_dir');
            foreach ($dirs as $dir) {
                $fileName = basename($dir);

                if ($fileName == 'themes') {
                    $pathFrom = $extractTo . '/themes';
                    $pathTo = get_home_path() . 'wp-content\themes';
                    Helper::moveDirectories($pathFrom, $pathTo);
                    Helper::removeDirectories($pathFrom);
                } else if ($fileName == 'files') {
                    $pathFrom = $extractTo . '/files';
                    $pathTo = get_home_path() . 'wp-content\uploads';
                    Helper::moveDirectories($pathFrom, $pathTo);
                    Helper::removeDirectories($pathFrom);
                } else if ($fileName == 'plugins') {
                    $pathFrom = $extractTo . '/plugins';
                    $pathTo = get_home_path() . 'wp-content\plugins';
                    Helper::moveDirectories($pathFrom, $pathTo);
                    Helper::removeDirectories($pathFrom);
                } else if ($fileName == 'database') {
                    $pathFrom = $extractTo . '/database';
                    $filename = array_filter(glob($pathFrom . '/*'));
                    Helper::DelDatabse();
                    Helper::importDatabase($filename[0]);
                    Helper::removeDirectories($pathFrom);
                }
            }
            echo 'Done!';
        } else {
            echo $res;
        }

Don't know what is the issue or what might be causing this strange issue any help would be appreciated. Thank you.

Possible Duplication

Unzip The Zip Archive With PHP

ZipArchive not opening file - Error Code: 19

But none of the solution is working.


Solution

  • Found the solution just in case anyone needed help in the future the real issue was not in the extraction code the issue was in the code from where I am downloading that zip file the real reason was that I was adding the downloaded content to the zip file with fwrite and fwrite wasn't compressing the content properly so if any one wants to download the zip file from google drive he/she can use this code it will work.

    $client = new Google_Client();
        $client->setClientId('CLIENT_ID');
        $client->setClientSecret('CLIENT_SECRET');
        $client->setScopes(array('https://www.googleapis.com/auth/drive'));
    
        $client->setAccessToken($AccessToken);
    
        $service = new Google_Service_Drive($client);
    
        $fileId = $dir['drive_dir'];
        $storeIn = wp_upload_dir()['basedir'] . '/' . $fileName . '.zip';
    
        $content = $service->files->get($fileId, array('alt' => 'media'));
    
        $headers = $content->getHeaders();
        foreach ($headers as $name => $values) {
            header($name . ': ' . implode(', ', $values));
        }
        header('Content-Disposition: attachment; filename="' . $fileName . '"');
    
        $body = $content->getBody();
    
        while (!$body->eof()) {
            file_put_contents($storeIn, $body);
        }
    
        echo "Done.\n";