I have a foldername afile, inside this folder I have checkfiles.php and allFiles folder which contains two zip files. Actually I am trying to unzip the two zip files, looping those zip files in foreach loop and want to open those folders and the folder contains config.xml file.
In this config.xml file I want to replace some text. I have written a code in checkfiles.php file and executing in the command prompt but it throwing error like Invalid or uninitialized Zip object where extractTo() function is written. I am using Ubuntu OS.
Here is my code
<?php
$folderName = 'allFiles';
$dir = opendir($folderName);
if($dir)
{
while( ($file_name = readdir($dir))!== FALSE )
{
if($file_name != '.' && $file_name != '..')
{
$file[] = $file_name;
}
}
}
foreach ($file as $allfile) {
$zip = new ZipArchive();
$res = $zip->open($allfile);
$zip->extractTo($folderName);
$existFile = file_exists('config.xml');
if($existFile)
{
$fileForWrite = fopen("config.xml","w") or die("Unable to open file!");
while(!feof($fileForWrite)) {
//Check highlightalpha="0.6" exists then replace with highlightalpha="0.3"
$file_contents = file_get_contents($fileForWrite);
$file_contents_replaced = str_replace('highlightalpha="0.6"','highlightalpha="0.3"',$file_contents);
file_put_contents($fileForWrite,$file_contents_replaced);
fclose($fileForWrite);
echo fgets($fileForWrite) . "<br>";
}
fclose($fileForWrite);
}
}