我想解压缩一个文件,这工作得很好
system('unzip File.zip');
但我需要通过URL传递文件名,不能让它工作,这就是我所拥有的。
$master = $_GET["master"];
system('unzip $master.zip');
我错过了什么?我知道一定是我忽视的小而愚蠢的事情。
谢谢你!
我想解压缩一个文件,这工作得很好
system('unzip File.zip');
但我需要通过URL传递文件名,不能让它工作,这就是我所拥有的。
$master = $_GET["master"];
system('unzip $master.zip');
我错过了什么?我知道一定是我忽视的小而愚蠢的事情。
谢谢你!
当前回答
我更新了@rdlowrey的答案,以一个更干净和更好的代码,这将解压文件到当前目录使用__DIR__。
<?php
// config
// -------------------------------
// only file name + .zip
$zip_filename = "YOURFILENAME.zip";
?>
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' >
<title>Unzip</title>
<style>
body{
font-family: arial, sans-serif;
word-wrap: break-word;
}
.wrapper{
padding:20px;
line-height: 1.5;
font-size: 1rem;
}
span{
font-family: 'Consolas', 'courier new', monospace;
background: #eee;
padding:2px;
}
</style>
</head>
<body>
<div class="wrapper">
<?php
echo "Unzipping <span>" .__DIR__. "/" .$zip_filename. "</span> to <span>" .__DIR__. "</span><br>";
echo "current dir: <span>" . __DIR__ . "</span><br>";
$zip = new ZipArchive;
$res = $zip->open(__DIR__ . '/' .$zip_filename);
if ($res === TRUE) {
$zip->extractTo(__DIR__);
$zip->close();
echo '<p style="color:#00C324;">Extract was successful! Enjoy ;)</p><br>';
} else {
echo '<p style="color:red;">Zip file not found!</p><br>';
}
?>
End Script.
</div>
</body>
</html>
其他回答
我更新了Morteza Ziaeemehr的答案,以一个更干净和更好的代码,这将解压一个文件提供的形式到当前目录使用DIR。
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' >
<title>Unzip</title>
<style>
body{
font-family: arial, sans-serif;
word-wrap: break-word;
}
.wrapper{
padding:20px;
line-height: 1.5;
font-size: 1rem;
}
span{
font-family: 'Consolas', 'courier new', monospace;
background: #eee;
padding:2px;
}
</style>
</head>
<body>
<div class="wrapper">
<?php
if(isset($_GET['page']))
{
$type = $_GET['page'];
global $con;
switch($type)
{
case 'unzip':
{
$zip_filename =$_POST['filename'];
echo "Unzipping <span>" .__DIR__. "/" .$zip_filename. "</span> to <span>" .__DIR__. "</span><br>";
echo "current dir: <span>" . __DIR__ . "</span><br>";
$zip = new ZipArchive;
$res = $zip->open(__DIR__ . '/' .$zip_filename);
if ($res === TRUE)
{
$zip->extractTo(__DIR__);
$zip->close();
echo '<p style="color:#00C324;">Extract was successful! Enjoy ;)</p><br>';
}
else
{
echo '<p style="color:red;">Zip file not found!</p><br>';
}
break;
}
}
}
?>
End Script.
</div>
<form name="unzip" id="unzip" role="form">
<div class="body bg-gray">
<div class="form-group">
<input type="text" name="filename" class="form-control" placeholder="File Name (with extension)"/>
</div>
</div>
</form>
<script type="application/javascript">
$("#unzip").submit(function(event) {
event.preventDefault();
var url = "function.php?page=unzip"; // the script where you handle the form input.
$.ajax({
type: "POST",
url: url,
dataType:"json",
data: $("#unzip").serialize(), // serializes the form's elements.
success: function(data)
{
alert(data.msg); // show response from the php script.
document.getElementById("unzip").reset();
}
});
return false; // avoid to execute the actual submit of the form
});
</script>
</body>
</html>
使用getcwd()在同一目录中进行解压缩
<?php
$unzip = new ZipArchive;
$out = $unzip->open('wordpress.zip');
if ($out === TRUE) {
$unzip->extractTo(getcwd());
$unzip->close();
echo 'File unzipped';
} else {
echo 'Error';
}
?>
可以使用预包装函数
function unzip_file($file, $destination){
// create object
$zip = new ZipArchive() ;
// open archive
if ($zip->open($file) !== TRUE) {
return false;
}
// extract contents to destination directory
$zip->extractTo($destination);
// close archive
$zip->close();
return true;
}
如何使用它。
if(unzip_file($file["name"],'uploads/')){
echo 'zip archive extracted successfully';
}else{
echo 'zip archive extraction failed';
}
PHP有自己的内置类,可用于解压缩或从zip文件中提取内容。这个类是ZipArchive。 下面是提取zip文件并将其放入特定目录的简单和基本的PHP代码:
<?php
$zip_obj = new ZipArchive;
$zip_obj->open('dummy.zip');
$zip_obj->extractTo('directory_name/sub_dir');
?>
如果你想要一些高级功能,那么下面是改进的代码,它将检查zip文件是否存在:
<?php
$zip_obj = new ZipArchive;
if ($zip_obj->open('dummy.zip') === TRUE) {
$zip_obj->extractTo('directory/sub_dir');
echo "Zip exists and successfully extracted";
}
else {
echo "This zip file does not exists";
}
?>
来源:如何解压缩或提取zip文件在PHP?
我更新了@rdlowrey的答案,以一个更干净和更好的代码,这将解压文件到当前目录使用__DIR__。
<?php
// config
// -------------------------------
// only file name + .zip
$zip_filename = "YOURFILENAME.zip";
?>
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' >
<title>Unzip</title>
<style>
body{
font-family: arial, sans-serif;
word-wrap: break-word;
}
.wrapper{
padding:20px;
line-height: 1.5;
font-size: 1rem;
}
span{
font-family: 'Consolas', 'courier new', monospace;
background: #eee;
padding:2px;
}
</style>
</head>
<body>
<div class="wrapper">
<?php
echo "Unzipping <span>" .__DIR__. "/" .$zip_filename. "</span> to <span>" .__DIR__. "</span><br>";
echo "current dir: <span>" . __DIR__ . "</span><br>";
$zip = new ZipArchive;
$res = $zip->open(__DIR__ . '/' .$zip_filename);
if ($res === TRUE) {
$zip->extractTo(__DIR__);
$zip->close();
echo '<p style="color:#00C324;">Extract was successful! Enjoy ;)</p><br>';
} else {
echo '<p style="color:red;">Zip file not found!</p><br>';
}
?>
End Script.
</div>
</body>
</html>