php - Passing path in extractTo function displaying error:Invalid or uninitialized Zip object in PHP
问题描述
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);
}
}
解决方案
推荐阅读
- c++ - 如何访问其他 .cc 文件中命名空间中定义的函数
- html - 如何使 Bootstrap 按钮适合 Bootstrap 表格单元格内
- git - Github 将现有的 repo 转换为 fork
- angular - 是否可以从 Create React App 或 Vue CLI 创建内存树?
- bash - Bash alias to wrap around Git commit
- javascript - body-parser json() 和 urlencoded() 函数是否替换了 express json() 和 urlencoded() 函数?
- ngrx - 应该在哪里创建/分配新数据对象的“id”?
- c++ - Are there any platform-specific limitations to `std::this_thread::sleep_for()`?
- ios - Location sensor not giving location on ios when enabled on Delphi
- javascript - 滚动到滚动事件的位置