2012-01-17 218 views
144

我要解压缩文件,并能正常工作解压缩文件用PHP

system('unzip File.zip'); 

但我需要通过URL中的文件名来传递并不能得到它的工作,这就是我。

$master = $_GET["master"]; 
system('unzip $master.zip'); 

我错过了什么?我知道它必须是我忽略的小而愚蠢的东西。

谢谢

+32

双引号评估变量;单引号不。但是 - 要小心,因为简单地将某些输入传递给系统调用可能相当危险。 – Wiseguy 2012-01-17 02:40:38

+23

必须说我不喜欢某些评论的敌意基调。如果你们认识到她/他错过了什么,那就告诉她/他。我们都会错过某些事情。 – 2015-03-11 08:27:10

+0

这里的错误很简单。您正尝试使用简单引号而不是双引号的字符串插值。字符串插值不适用于简单的引号,因为它用于字符串文字。因此,将你的代码改为'system(“unzip $ master.zip”);'应该可以。 – 2017-02-13 10:45:05

回答

367

我只能假设你的代码来自某个在线教程?在那种情况下,好好努力自己解决问题。另一方面,这段代码实际上可能在某处发布,因为解压文件的正确方法有点令人恐惧。

PHP内置了处理压缩文件的扩展。应该不需要使用system呼叫。 ZipArchivedocs是一种选择。

$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版本...这是一个reeeeeally很久以前弃用。不要使用它。改为使用$_GET超全局。

最后,要非常小心地接受通过$_GET变量传递给脚本的任何输入。

始终使用SANITIZE USER INPUT。


UPDATE

根据您的评论,解压压缩文件到它所在的同一目录下最好的办法是确定硬盘文件的路径和具体解压到位置。所以,你可以这样做:

// 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"; 
} 
+0

谢谢你的建议。我对此很陌生,只是把事情搞清楚。使用你的代码,我怎样才能将它解压缩到压缩文件所在的文件夹中? – BostonBB 2012-01-17 03:06:26

+2

那么,你的脚本的当前工作目录和zip文件所在的目录是有区别的。如果zip文件与脚本位于相同的目录中,您可以执行'$ zip-> extractTo('./');'但是,情况可能并非如此。更好的选择是确定zip文件在文件系统中的位置并将其提取出来。我会更新我的答案来演示。 – rdlowrey 2012-01-17 03:40:31

+0

如果您没有ZipArchive类可用,该怎么办?我在一个垃圾托管的网站上工作,我得到'致命的错误:班'ZipArchive'找不到'错误,我尝试这个脚本:-(有没有在这一点的选项? – CWSpear 2012-08-10 17:47:41

25

请不要那样做(通过GET变种是一个系统调用的一部分)。改为使用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 
} 

并回答你的问题,你的错误是“什么是$ var别的东西”应该是“什么是$ var别的东西”(双引号)。

+3

+1哎呀,对不起,五分钟后基本上可以找到相同的答案。没有看到你:) – rdlowrey 2012-01-17 02:57:16

4

我更新了答案@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> 
0

只要改变

system('unzip $master.zip'); 

这一个

system('unzip ' . $master . '.zip'); 

或这一个

system("unzip {$master}.zip");

+6

虽然这可能会解决问题他有,看看其他答案,看看为什么这是不好的建议。 – rjdown 2014-10-22 22:39:00

-1

只要使用此:

$master = $_GET["master"]; 
    system('unzip' $master.'.zip'); 
在你的代码 $master

作为字符串传递,系统将寻找一个名为$master.zip

$master = $_GET["master"]; 
    system('unzip $master.zip'); `enter code here` 
+3

完全错误。 ''unzip'$ master'将不起作用,除非您在解压缩后添加了一个空格并在单引号后添加了一段时间。用双引号或者至少一个有效的答案来建议'system(“unzip $ master.zip”)''会容易得多。 – 2015-03-27 01:36:19

+0

使用PHP系统函数比依赖系统函数更好。 – 2017-03-29 12:45:24

4

只要试试这个yourDestinationDir是目标提取或删除-d yourDestinationDir提取到根目录。

$master = 'someDir/zipFileName'; 
$data = system('unzip -d yourDestinationDir '.$master.'.zip'); 
+1

我使用'yourDestinationDir'作为'session_save_path()。 DIRECTORY_SEPARATOR。 “$ name”。 DIRECTORY_SEPARATOR'其中'$ name'是要在会话文件夹中解压缩的目标文件夹。 – 2016-02-21 10:41:52

+0

在处理包含变量的系统或exec函数时务必小心!它可以从服务器向黑客赋予免费的命令行。 – maxime 2016-09-14 17:06:21

+0

@maxime这怎么可能?请解释一下,因为我认为php是阻止这种情况发生的措施。用事实回复您的索赔 – 2016-09-26 01:55:50

0

我更新的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> 
0

您可以使用预装的功能

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'; 
} 
0
function extract_zip($Sourse_file, $extract_folder){ 
    $zip = new ZipArchive() ; 
    if (!$zip->open($Sourse_file) == TRUE) { 
     return false; 
    } 
    $zip->extractTo($extract_folder); 
    $zip->close(); 
     return true; 
} 
+1

描述你的答案请 – IsuruAb 2017-03-13 04:00:43

0

使用下面的PHP代码,用文件名中的URL参数“名称”

<?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'; 
} 

?> 
0

PHP具有可用于解压缩或从zip中提取内容自身的内在类文件。该类是ZipArchive。 下面是简单和基本的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"; 
} 
?> 

来源:How to unzip or extract zip files in PHP?

0

使用getcwd()在同一个目录中提取

<?php 
$unzip = new ZipArchive; 
$out = $unzip->open('wordpress.zip'); 
if ($out === TRUE) { 
    $unzip->extractTo(getcwd()); 
    $unzip->close(); 
    echo 'File unzipped'; 
} else { 
    echo 'Error'; 
} 
?>