Reputation: 81
I have installed the latest version of this library using Composer. I created a test file to test the library's functionality, but when I try to parse a file and make a data array out of it, I get this error;
Fatal error: Uncaught PhpOffice\PhpSpreadsheet\Reader\Exception: Could not find zip member zip:///home/klik/tmp/php4cfRJH#_rels/.rels in /home/klik/scripts/PhpSpreadsheet-master/src/PhpSpreadsheet/Shared/File.php:159
Stack trace:
#0 /home/klik/scripts/PhpSpreadsheet-master/src/PhpSpreadsheet/Reader/Xlsx.php(392): PhpOffice\PhpSpreadsheet\Shared\File::assertFile('/home/klik/tmp/...', '_rels/.rels')
#1 /home/klik/scripts/PhpSpreadsheet-master/test.php(37): PhpOffice\PhpSpreadsheet\Reader\Xlsx->load('/home/klik/tmp/...')
#2 {main} thrown in /home/klik/scripts/PhpSpreadsheet-master/src/PhpSpreadsheet/Shared/File.php on line 159
This is my test.php
file:
<?php
echo '<!DOCTYPE html>
<html>
<body>
<form method="post" action="test.php" enctype="multipart/form-data">
<div class="form-group">
<label for="exampleInputFile">File Upload</label>
<input type="file" name="file" class="form-control" id="exampleInputFile">
</div>
<button type="submit" name="submit" class="btn btn-primary">Submit</button>
</form>
</body>
</html>';
require_once 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Reader\Csv;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
if (isset($_POST['submit'])) {
$file_mimes = array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel', 'text/plain', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
if(isset($_FILES['file']['name']) && in_array($_FILES['file']['type'], $file_mimes)) {
$arr_file = explode('.', $_FILES['file']['name']);
$extension = end($arr_file);
if('csv' == $extension) {
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Csv();
} else {
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
}
$spreadsheet = $reader->load($_FILES['file']['tmp_name']);
$sheetData = $spreadsheet->getActiveSheet()->toArray();
print_r($sheetData);exit;
}
}
?>
I am using the simplest .xls
Excel file for processing.
PHP 7.4.14 (cli) (built: Jan 30 2021 20:03:43) ( ZTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies
Upvotes: 8
Views: 27660
Reputation: 1
You must use load static of function \PhpOffice\PhpSpreadsheet\IOFactory instead PhpOffice\PhpSpreadsheet\Reader\Xlsx.
Upvotes: 0
Reputation: 87
I implemented this in my laravel application and i got the same errors.
It solved by specifying the full path of the file.
In my case: it is e.g., \Excel::import(new MatchingImport, 'full_path_of_the_file_here');
In your case, try to add full path on line: $spreadsheet = $reader->load($_FILES['file']['tmp_name']);
$spreadsheet = $reader->load('full_path_of_the_file_here');
Upvotes: 0
Reputation: 79
if('csv' == $extension) {
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Csv();
} else if('xls' == $extension) {
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xls();
} else
$reader = new \PhpOffice\PhpSpreadsheet\Reader\Xlsx();
Upvotes: 7