![](/img/trans.png)
[英]PHPExcel Warning: ZipArchive::getFromName(): Invalid or unitialized Zip object in
[英]codeigniter phpexcel error ZipArchive::getFromName(): Invalid or uninitialized Zip object
我正在尝试使用codeigniter和phpexcel从excel文件(.xlsx)将数据导入oracle,这是我的控制器:
private $filename;
public function form(){
$data = array();
if(isset($_POST['preview'])){
$upload = $this->RoadmapModel->upload_file($this->filename);
$upload_data = $this->upload->data();
$this->filename = $upload_data['file_name'];
if($upload['result'] == "success"){
include APPPATH.'third_party/PHPExcel/PHPExcel.php';
$excelreader = new PHPExcel_Reader_Excel2007();
$loadexcel = $excelreader->load('excel/'.$this->filename);
$sheet = $loadexcel->getActiveSheet()->toArray(null, true, true ,true);
$data['sheet'] = $sheet;
}else{ // Jika proses upload gagal
$data['upload_error'] = $upload['error'];
}
}
$this->load->view('form', $data);
}
public function import(){
include APPPATH.'third_party/PHPExcel/PHPExcel.php';
$excelreader = new PHPExcel_Reader_Excel2007();
$loadexcel = $excelreader->load('excel/'.$this->filename = $this -> form());
$sheet = $loadexcel->getActiveSheet()->toArray(null, true, true ,true);
$data = [];
$numrow = 1;
foreach($sheet as $row){
if($numrow > 1){
// Kita push (add) array data ke variabel data
array_push($data, [
'TAHUN'=>$row['A'],
'PROVINCEID'=>$row['B'],
'PROVINSI'=>$row['C'],
'PLAN_DESAB'=>$row['D'],
'ACTUAL_DESAB'=>$row['E'],
'PLAN_ELEKTRIFIKASI'=>$row['F'],
'ACTUAL_ELEKTRIFIKASI'=>$row['G'],
'PLAN_LISDES'=>$row['H'],
'ACTUAL_LISDES'=>$row['I'],
]);
}
$numrow++;
}
$this->RoadmapModel->insert_multiple($data);
redirect("Roadmap");
}
这是我的模型:
public $tablename = "X";
function upload_file($filename){
$this->load->library('upload');
$config['upload_path'] = './excel/';
$config['allowed_types'] = 'xlsx';
$config['max_size'] = '2048';
$config['overwrite'] = true;
$config['file_name'] = $filename;
$this->upload->initialize($config);
if($this->upload->do_upload('file')){
$return = array('result' => 'success', 'file' => $upload_data = $this->upload->data(), 'error' => '');
return $return;
}else{
$return = array('result' => 'failed', 'file' => '', 'error' => $this->upload->display_errors());
return $return;
}
}
function insert_multiple($data){
$p_tablename= $this->tablename;
$this->db->insert_batch($p_tablename, $data);
}
当我使用导入功能时,这是错误消息:
消息:ZipArchive::getFromName():无效或未初始化的 Zip 对象
文件名:Reader/Excel2007.php
行号:327
回溯:
文件:C:\\xampp\\htdocs\\web_excel_ci\\application\\controllers\\Roadmap.php
线路:82
功能:加载
第 82 行是$loadexcel = $excelreader->load('excel/'.$this->filename = $this -> form());
在函数导入()
那是为了加载要导入的excel文件,我尝试使用$this->filename = $this->form()
从函数 form() 获取文件名,但这是错误的
请帮助解决问题,因为我已经堆在那里
非常感谢...
默认情况下,PHPWord 使用 Zip 扩展名来处理 ZIP 压缩档案和其中的文件。 如果您的服务器上无法安装 Zip 扩展,则可以使用 PHPWord 中包含的纯 PHP 库替代品 PclZip。
\PhpOffice\PhpWord\Settings::setZipClass(\PhpOffice\PhpWord\Settings::PCLZIP);
这对我有用。
线路肯定有问题
$loadexcel = $excelreader->load('excel/'.$this->filename = $this->form());
它似乎试图用$this->form()
的返回值设置$this->filename
。 但是$this->form()
不返回值 - 它加载一个视图。
因此,传递给$excelreader->load()
的参数可能只是字符串“excel/”。 确定这不是有效的 zip 对象。
此序列应为excelreader->load()
生成正确的字符串。
$this->form();
$loadexcel = $excelreader->load('excel/'.$this->filename);
但是您必须接受视图也将被加载。
在此堆栈溢出中搜索了许多小时后,我得到了解决此情况的解决方案,
我使用$this->session->set_flashdata('fileName',$fileName);
获取一个函数中的值
并使用$fileName = $this->session->flashdata('fileName');
将该值放入另一个函数中
它奏效了。
谢谢大家的关注...
与使用 PHPWord 并收到相同错误消息的人没有直接关系,但可能会引起他们的兴趣:
当您使用“.doc”文件而不是“.docx”时,PHPWord 也会出现相同类型的问题。
将所有“.doc”文件转换为“.docx”文件,特别是如果您想使用仅支持“.docx”文件的 TemplateProcessor 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.