zip_close() 期望参数 1 是第 37 行给出的资源、对象
zip_close() expects parameter 1 to be resource, object given on line 37
我正在创建一个文件来提取 zip 文件。截断的代码检查文件扩展名是否为 .zip,然后检查文件是否已经存在。如果没有,请提取存档。
if($fileExtension == "zip")
{
if(is_dir("unzips/".$fileName) == false)
{
move_uploaded_file($_FILES["file"]["tmp_name"],"temp/".$_FILES["file"]["name"]);
$zip = new ZipArchive();
$zip -> open("temp/".$_FILES["file"]["name"]);
for($num = 0; $num < $zip->numFiles; $num++)
{
$fileInfo = $zip->statIndex($num);
echo "Successfully Extracted: ".$fileInfo["name"];
$zip->extractTo("unzips/".$fileName);
echo "<br />";
}
zip_close($zip);
unlink("temp/".$_FILES["file"]["name"]);
}
else
{
echo $fileName." The ZIP archive file has already been unzipped";
}
}
else
{
echo "Only .zip files are allowed";
}
这是 index.php
的完整代码
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Unzipping Zip File</title>
</head>
<body>
<form action="index.php" method="POST" enctype="multipart/form-data">
<input type="file" name="file"><input type="submit" name="submit" value="Extract">
</form>
<?php
if(isset($_POST['submit']))
{
$array = explode(".",$_FILES["file"]["name"]);
$fileName = $array[0];
$fileExtension = strtolower(end($array));
if($fileExtension == "zip")
{
if(is_dir("unzips/".$fileName) == false)
{
move_uploaded_file($_FILES["file"]["tmp_name"],"temp/".$_FILES["file"]["name"]);
$zip = new ZipArchive();
$zip -> open("temp/".$_FILES["file"]["name"]);
for($num = 0; $num < $zip->numFiles; $num++)
{
$fileInfo = $zip->statIndex($num);
echo "Successfully Extracted: ".$fileInfo["name"];
$zip->extractTo("unzips/".$fileName);
echo "<br />";
}
$zip -> close();
unlink("temp/".$_FILES["file"]["name"]);
}
else
{
echo $fileName." The ZIP archive file has already been unzipped";
}
}
else
{
echo "Only .zip files are allowed";
}
}
?>
<br />
<p id="end note">Please return to main menu and download individually extracted files.</p>
</body>
</html>
目录结构如下:
D:/MyWebs/extracting/index.php
D:/MyWebs/extracting/unzips(包含解压文件夹的最终目的地)
D:/MyWebs/extracting/temp(存储zip然后删除)
void zip_close ( resource $zip )
zip
A ZIP file previously opened with zip_open().
http://php.net/manual/en/function.zip-close.php
$zip
是 ZipArchive()
class 的实例。因此,您不能将其传递给 zip_close()
,因为它需要使用 zip_open()
.
创建的资源
................................................ ...................................
编辑:我注意到在添加完整源代码后您的代码中还有另一个错误。
您为 $fileName
变量设置了无效值。
$array = explode(".",$_FILES["file"]["name"]);
$fileName = $array[0];
分解 $_FILES["file"]["name"]
并仅获得 $array[0]
值时,您仅获得文件名(不带扩展名)。
因此,当您将无效路径传递给 $zip->open($fileName);
方法时,它会 return 您出错:
ZipArchive::close(): Invalid or uninitialized Zip object
将上面的内容更改为:
$array = explode(".",$_FILES["file"]["name"]);
$extractDir = $array[0];
$fileName = $_FILES["file"]["name"];
$fileExtension = strtolower(end($array));
if(is_dir("unzips/".$extractDir) == false){
.
.
$zip->extractTo("unzips/".$extractDir);
.
.
}
这行得通。
希望对您有所帮助:)
zip_close()
expects parameter 1 to be resource, object given
错误消息不言自明。
documentation of zip_close()
表示 zip_close()
期望的参数应该是:
A ZIP file previously opened with zip_open().
因为您使用的是 ZipArchive
object you have to use its methods to handle it. In particular, ZipArchive::close()
is the method used to close a ZipArchive
(previously open using ZipArchive::open()
。
您的代码应如下所示:
$zip = new ZipArchive();
$zip->open("temp/".$_FILES["file"]["name"]);
// Do whatever you want with the content of the archive here (extract files, f.e.)
$zip->close();
我正在创建一个文件来提取 zip 文件。截断的代码检查文件扩展名是否为 .zip,然后检查文件是否已经存在。如果没有,请提取存档。
if($fileExtension == "zip")
{
if(is_dir("unzips/".$fileName) == false)
{
move_uploaded_file($_FILES["file"]["tmp_name"],"temp/".$_FILES["file"]["name"]);
$zip = new ZipArchive();
$zip -> open("temp/".$_FILES["file"]["name"]);
for($num = 0; $num < $zip->numFiles; $num++)
{
$fileInfo = $zip->statIndex($num);
echo "Successfully Extracted: ".$fileInfo["name"];
$zip->extractTo("unzips/".$fileName);
echo "<br />";
}
zip_close($zip);
unlink("temp/".$_FILES["file"]["name"]);
}
else
{
echo $fileName." The ZIP archive file has already been unzipped";
}
}
else
{
echo "Only .zip files are allowed";
}
这是 index.php
的完整代码 <!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Unzipping Zip File</title>
</head>
<body>
<form action="index.php" method="POST" enctype="multipart/form-data">
<input type="file" name="file"><input type="submit" name="submit" value="Extract">
</form>
<?php
if(isset($_POST['submit']))
{
$array = explode(".",$_FILES["file"]["name"]);
$fileName = $array[0];
$fileExtension = strtolower(end($array));
if($fileExtension == "zip")
{
if(is_dir("unzips/".$fileName) == false)
{
move_uploaded_file($_FILES["file"]["tmp_name"],"temp/".$_FILES["file"]["name"]);
$zip = new ZipArchive();
$zip -> open("temp/".$_FILES["file"]["name"]);
for($num = 0; $num < $zip->numFiles; $num++)
{
$fileInfo = $zip->statIndex($num);
echo "Successfully Extracted: ".$fileInfo["name"];
$zip->extractTo("unzips/".$fileName);
echo "<br />";
}
$zip -> close();
unlink("temp/".$_FILES["file"]["name"]);
}
else
{
echo $fileName." The ZIP archive file has already been unzipped";
}
}
else
{
echo "Only .zip files are allowed";
}
}
?>
<br />
<p id="end note">Please return to main menu and download individually extracted files.</p>
</body>
</html>
目录结构如下: D:/MyWebs/extracting/index.php D:/MyWebs/extracting/unzips(包含解压文件夹的最终目的地) D:/MyWebs/extracting/temp(存储zip然后删除)
void zip_close ( resource $zip )
zip
A ZIP file previously opened with zip_open().
http://php.net/manual/en/function.zip-close.php
$zip
是 ZipArchive()
class 的实例。因此,您不能将其传递给 zip_close()
,因为它需要使用 zip_open()
.
................................................ ...................................
编辑:我注意到在添加完整源代码后您的代码中还有另一个错误。
您为 $fileName
变量设置了无效值。
$array = explode(".",$_FILES["file"]["name"]);
$fileName = $array[0];
分解 $_FILES["file"]["name"]
并仅获得 $array[0]
值时,您仅获得文件名(不带扩展名)。
因此,当您将无效路径传递给 $zip->open($fileName);
方法时,它会 return 您出错:
ZipArchive::close(): Invalid or uninitialized Zip object
将上面的内容更改为:
$array = explode(".",$_FILES["file"]["name"]);
$extractDir = $array[0];
$fileName = $_FILES["file"]["name"];
$fileExtension = strtolower(end($array));
if(is_dir("unzips/".$extractDir) == false){
.
.
$zip->extractTo("unzips/".$extractDir);
.
.
}
这行得通。 希望对您有所帮助:)
zip_close()
expects parameter 1 to be resource, object given
错误消息不言自明。
documentation of zip_close()
表示 zip_close()
期望的参数应该是:
A ZIP file previously opened with zip_open().
因为您使用的是 ZipArchive
object you have to use its methods to handle it. In particular, ZipArchive::close()
is the method used to close a ZipArchive
(previously open using ZipArchive::open()
。
您的代码应如下所示:
$zip = new ZipArchive();
$zip->open("temp/".$_FILES["file"]["name"]);
// Do whatever you want with the content of the archive here (extract files, f.e.)
$zip->close();