我需要能够提取 zip 文件,并将 zip 文件中的文件名插入数据库。
我四处搜索,发现了多个解压缩 zip 文件的脚本,但似乎我无法获取 zip 文件中文件的各个文件名。
我可能会建议使用两个系统调用:
1)解压缩到 tmp 文件夹 2)从该文件夹中读取文件
(更新的完整工作脚本):
<?php
if (isset($_POST["fsubmit"])){
echo "<pre>";
var_dump($_POST);
var_dump($_FILES);
$tmp_dir = "tmp/" . microtime(true);
if (!file_exists($tmp_dir)){
mkdir($tmp_dir);
}
if (is_uploaded_file($_FILES["file"]["tmp_name"])){
move_uploaded_file($_FILES["file"]["tmp_name"], $tmp_dir . "/a.zip");
exec("unzip -z -j $tmp_dir $tmp_dir" . "/a.zip");
exec("ls $tmp_dir", $out);
echo "Files in the archive:\n";
foreach ($out as $file){
$file = trim($file);
echo "File: $file,", filesize($tmp_dir . "/" . $file)."b\n";
}
exec("rm -rf $tmp_dir");
}
} else {
?>
<form action="test.php" method="POST" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" name="fsubmit" value="upload">
</form>
<?
}
先决条件
0) 如果你在 unix/linux/mac 下可以工作(在 windows 下不能工作)
1) 创建 test.php 并将上面的代码粘贴到其中
2) 确保在 test.php 所在的同一文件夹中,是文件夹 tmp
3) 确保 apache 用户可以写入该文件夹(例如 chmod 777 tmp)
4) 确保您的 php 允许文件上传
5) 确保您的服务器具有“解压缩”命令行工具
问候。