Search code examples
phplinuxcentosphp-ziparchive

ziparchive() issue in server


I am not able to create a .zip file using ziparchive() I am using below piece of code

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

if ($zip->open($filename, ZipArchive::CREATE)!==TRUE) {
    echo ("<script LANGUAGE='JavaScript'> window.alert('Error opening zip archive...!'); window.location.href='http://work.chaturlocal.com/shop/<?php echo $shop_id; ?>'; </script>");
}

$dir = 'includes/';

// Create zip
createZip($zip,$dir);

$zip->close();

function createZip($zip,$dir){
if (is_dir($dir)){

    if ($dh = opendir($dir)){
        while (($file = readdir($dh)) !== false){

            // If file
            if (is_file($dir.$file)) {
                if($file != '' && $file != '.' && $file != '..'){

                    $zip->addFile($dir.$file);
                }
            }else{
                // If directory
                if(is_dir($dir.$file) ){

                    if($file != '' && $file != '.' && $file != '..'){

                        // Add empty directory
                        $zip->addEmptyDir($dir.$file);

                        $folder = $dir.$file.'/';

                        // Read data of the folder
                        createZip($zip,$folder);
                    }
                }

            }

        }
        closedir($dh);
    }
}
}

The code is working perfectly in localhost which has php version 7. But not working in server with php 5.4.1. I have installed the zip library using "php-zip", not able to execute the command "phpenmod zip". Kindly let me know the solution. Thank you.


Solution

  • Updated the php.ini as

    extension=zip.so
    

    Below the

    ; extension=msql.so