我尝试了很多代码,但没有工作。
<?php
$file = $_GET['file'];
if (isset($file))
{
echo "Unzipping " . $file . "<br>";
if(system('unzip '. $file.' -d dirtounzipto ' ))
{echo 'GGWP';}else{echo 'WTF';}
exit;
}?>
如何在服务器中解压缩。使用&#34; system&#34;或&#34; shell_exec&#34;代码。
答案 0 :(得分:4)
$zip_filename = "test.zip";
$zip_extract_path = "/";
try{
$zip_obj = new ZipArchive;
if (file_exists($zip_filename)) {
$zip_stat = $zip_obj->open($zip_filename);
if ($zip_stat === TRUE) {
$res = $zip_obj->extractTo($zip_extract_path);
if ($res === false) {
throw new Exception("Error in extracting file on server.");
}
$zip_obj->close();
} else {
throw new Exception("Error in open file");
}
} else {
throw new Exception("zip file not found for extraction");
}
}catch (Exception $e) {
echo $e->getMessage();
}
答案 1 :(得分:1)
请充分利用PHP的ZipArchive
library:
<?php
$zip = new ZipArchive;
if ($zip->open('test.zip') === TRUE) {
$zip->extractTo('/my/destination/dir/');
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
?>
版本要求:PHP&gt; = 5.2.0,PECL zip&gt; = 1.1.0
更新要自动创建目标路径,您可以使用:
mkdir($path, 0755, true);
自动创建所需的文件夹。
答案 2 :(得分:0)
PHP具有用于处理压缩文件的内置扩展。不需要为此使用系统调用。 ZipArchive docs是一种选择。
// 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);
//folder name as per zip file name
$foldername = basename($file, ".zip");
mkdir($foldername, 0755, true);
$path = $path . "/" . $foldername;
$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";
}