我想解压缩一个文件,这工作得很好
system('unzip File.zip');
但我需要通过URL传递文件名,不能让它工作,这就是我所拥有的。
$master = $_GET["master"];
system('unzip $master.zip');
我错过了什么?我知道一定是我忽视的小而愚蠢的事情。
谢谢你!
我想解压缩一个文件,这工作得很好
system('unzip File.zip');
但我需要通过URL传递文件名,不能让它工作,这就是我所拥有的。
$master = $_GET["master"];
system('unzip $master.zip');
我错过了什么?我知道一定是我忽视的小而愚蠢的事情。
谢谢你!
当前回答
使用下面的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';
}
?>
其他回答
请不要这样做(将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 something else”应该是“something $var something else”(双引号)。
简单地尝试这个yourDestinationDir是目的地提取到或删除-d yourDestinationDir提取到根目录。
$master = 'someDir/zipFileName';
$data = system('unzip -d yourDestinationDir '.$master.'.zip');
只改变
system('unzip $master.zip');
到这个
system('unzip ' . $master . '.zip');
或者这个
系统(“{$大师}. zip解压缩”);
使用下面的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';
}
?>
使用getcwd()在同一目录中进行解压缩
<?php
$unzip = new ZipArchive;
$out = $unzip->open('wordpress.zip');
if ($out === TRUE) {
$unzip->extractTo(getcwd());
$unzip->close();
echo 'File unzipped';
} else {
echo 'Error';
}
?>