我想解压一个文件,这很好用
system('unzip File.zip');
但是我需要通过URL传入文件名并且无法让它工作,这就是我所拥有的。
$master = $_GET["master"];
system('unzip $master.zip');
我错过了什么?我知道这一定是我忽略的一些小而愚蠢的事情。
谢谢你,
我只能假设您的代码来自在线教程?在这种情况下,你自己尝试解决这个问题就很好了。另一方面,这个代码实际上可以作为解压缩文件的正确方法在线发布,这一事实有点令人恐惧。
PHP 具有处理压缩文件的内置扩展。应该不需要为此使用
system
调用。 ZipArchive
文档是一种选择。
$zip = new ZipArchive;
$res = $zip->open('file.zip');
if ($res === TRUE) {
$zip->extractTo('/myzips/extract_path/');
$zip->close();
echo 'woot!';
} else {
echo 'doh!';
}
此外,正如其他人评论的那样,
$HTTP_GET_VARS
自 4.1 版以来已被弃用……那是很久以前的事了。不要使用它。请改用 $_GET
超全局变量。
最后,在接受通过
$_GET
变量传递给脚本的任何输入时要非常小心。
更新
根据您的评论,将 zip 文件提取到其所在目录的最佳方法是确定文件的硬路径并将其专门提取到该位置。所以,你可以这样做:
// assuming file.zip is in the same directory as the executing script.
$file = 'file.zip';
// get the absolute path to $file
$path = pathinfo(realpath($file), PATHINFO_DIRNAME);
$zip = new ZipArchive;
$res = $zip->open($file);
if ($res === TRUE) {
// extract it to the path we determined above
$zip->extractTo($path);
$zip->close();
echo "WOOT! $file extracted to $path";
} else {
echo "Doh! I couldn't open $file";
}
请不要这样做(传递 GET var 作为系统调用的一部分)。请使用 ZipArchive 来代替。
所以,你的代码应该如下所示:
$zipArchive = new ZipArchive();
$result = $zipArchive->open($_GET["master"]);
if ($result === TRUE) {
$zipArchive ->extractTo("my_dir");
$zipArchive ->close();
// Do something else on success
} else {
// Do something on error
}
为了回答你的问题,你的错误是“something $var some else”应该是“something $var some else”(用双引号引起来)。
使用
getcwd()
解压到同一目录
<?php
$unzip = new ZipArchive;
$out = $unzip->open('wordpress.zip');
if ($out === TRUE) {
$unzip->extractTo(getcwd());
$unzip->close();
echo 'File unzipped';
} else {
echo 'Error';
}
?>
我将 @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>
只需尝试此 yourDestinationDir 是提取到的目标或删除 -d yourDestinationDir 以提取到根目录。
$master = 'someDir/zipFileName';
$data = system('unzip -d yourDestinationDir '.$master.'.zip');
PHP 有自己的内置类,可用于解压缩或从 zip 文件中提取内容。该类是 ZipArchive。 下面是简单且基本的 PHP 代码,它将提取 zip 文件并将其放置在特定目录中:
<?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";
}
?>
简单的 PHP 解压函数。请确保您的服务器上安装了 zip 扩展名。
/**
* Unzip
* @param string $zip_file_path Eg - /tmp/my.zip
* @param string $extract_path Eg - /tmp/new_dir_name
* @return boolean
*/
function unzip(string $zip_file_path, string $extract_dir_path) {
$zip = new \ZipArchive;
$res = $zip->open($zip_file_path);
if ($res === TRUE) {
$zip->extractTo($extract_dir_path);
$zip->close();
return TRUE;
} else {
return FALSE;
}
}
我将 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>
改变一下
system('unzip $master.zip');
对于这个
system('unzip ' . $master . '.zip');
或者这个
system("unzip {$master}.zip");
您可以使用预装函数
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 代码,文件名位于 URL 参数“name”中
<?php
$fileName = $_GET['name'];
if (isset($fileName)) {
$zip = new ZipArchive;
$res = $zip->open($fileName);
if ($res === TRUE) {
$zip->extractTo('./');
$zip->close();
echo 'Extracted file "'.$fileName.'"';
} else {
echo 'Cannot find the file name "'.$fileName.'" (the file name should include extension (.zip, ...))';
}
}
else {
echo 'Please set file name in the "name" param';
}
?>
要在 PHP 中解压缩文件,您可以使用 zip_open() 和 zip_read() 函数打开并读取 zip 文件的内容,然后使用 file_put_contents() 将每个文件保存到您想要的位置。以下是如何使用 PHP 解压缩 zip 文件的示例:
<?php
// Path to the zip file you want to unzip
$zipFilePath = 'your_file.zip';
// Directory where you want to extract the contents
$extractedPath = 'extracted_files/';
// Create the extraction directory if it doesn't exist
if (!is_dir($extractedPath)) {
mkdir($extractedPath, 0777, true);
}
$zip = zip_open($zipFilePath);
if ($zip) {
while ($zip_entry = zip_read($zip)) {
$entryName = zip_entry_name($zip_entry);
$entrySize = zip_entry_filesize($zip_entry);
if (zip_entry_open($zip, $zip_entry, "r")) {
$contents = zip_entry_read($zip_entry, $entrySize);
zip_entry_close($zip_entry);
$file = $extractedPath . $entryName;
// Create directories if needed
$dir = dirname($file);
if (!is_dir($dir)) {
mkdir($dir, 0777, true);
}
// Save the extracted file
file_put_contents($file, $contents);
}
}
zip_close($zip);
echo "Zip file '$zipFilePath' has been successfully extracted to '$extractedPath'.";
} else {
echo "Failed to open the zip file '$zipFilePath'.";
}
?>
在此脚本中:
将 $zipFilePath 替换为 zip 文件的路径。 修改 $extractedPath 以指定要提取内容的目录。 如果解压目录不存在,脚本将创建该目录。 它打开 zip 文件,读取其内容,并将每个文件解压到指定目录。 确保您有适当的权限来读取 zip 文件并写入解压目录。
只需使用这个:
$master = $_GET["master"];
system('unzip' $master.'.zip');
在您的代码中
$master
作为字符串传递,系统将查找名为 $master.zip
的文件
$master = $_GET["master"];
system('unzip $master.zip'); `enter code here`