if ($_SERVER["REQUEST_METHOD"] === "POST") {
if (isset($_FILES["file"]["tmp_name"]) && !empty($_FILES["file"]["tmp_name"])) {
$file = $_FILES["file"]["tmp_name"];
$file_name = $_FILES["file"]["name"];
$zip = new ZipArchive();
if ($zip->open($file) === TRUE) {
echo "the zip archive has been opened <br>";
$desiredFileName = "Week1_2019.xlsx";
if ($zip->locateName($desiredFileName) !== false) {
echo "the zip file has been located <br>";
$full_path = "zip://$file#$desiredFileName";
try {
$spreadsheet = IOFactory::load($full_path);
} catch (PhpOffice\PhpSpreadsheet\Reader\Exception $e) {
echo 'Error loading file: ' . $e->getMessage();
}
}
$zip->close();
}
}
}
I am trying to parse excel files within a zip file using PhpSpreadsheet. Currently this is my output:
我正在尝试使用PhpSpreadSheet解析压缩文件中的EXCEL文件。目前,这是我的输出:
the zip archive has been opened
the zip file has been located
Error loading file: File "zip://C:\xampp\tmp\php4229.tmp#Week1_2019.xlsx" does not exist.
I have also tried supplying the raw file name as a string i.e $desiredFileName which throws the same exception: Error loading file: File "Week1_2019.xlsx" does not exist.
我还尝试以字符串形式提供原始文件名,即$desiredFileName,它抛出相同的异常:加载文件时出错:文件“Week1_2019.xlsx”不存在。
$spreadsheet = IOFactory::load($desiredFileName);
更多回答
Try closing the zip before calling IOFactory::load()
.
在调用IOFactory::Load()之前尝试关闭压缩包。
优秀答案推荐
我是一名优秀的程序员,十分优秀!