【问题标题】:Can't unzip file with php无法用php解压文件
【发布时间】:2020-11-12 19:58:20
【问题描述】:

我的网络服务器中有一个文件夹,我放置了需要然后解压缩的 zip 文件。我想用 php 做到这一点,这是我尝试过的,但它不起作用:

<?php
$file = $_GET["file"];
$zip = new ZipArchive;
$res = $zip->open($file+'.zip');
$zip->extractTo('./');
$zip->close();
?>

zip 文件与 php 文件在同一个文件夹中,但是当我转到 php 页面时它什么也不做。

通过进行一些测试,我发现脚本在 $zip = new ZipArchive; 行上死了

我怎样才能让它发挥作用?

【问题讨论】:

  • the script dies 由于什么错误...?
  • @UmairKhan 我不知道。我只是在每一行之前放了一个回显,脚本在第一行代码之后停止回显

标签: php file unzip


【解决方案1】:

试试这个代码。还将$zip-&gt;open($file+".zip"); 更改为$zip-&gt;open($file);+(加号)不是php中的连接运算符

<?php
    // $_GET["file"] is set to `a.zip`

    $file = $_GET["file"];
    $zip = new ZipArchive;
    $res = $zip->open($file);
    $zip->extractTo('./');
    $zip->close();
?>

【讨论】:

    【解决方案2】:
    <?php
    
    $fileName = $_GET['file'];  // get file name in the URL param "file"
    
    if (isset($fileName)) {    // if $fileName php variable is set than    
    
    
        $zip = new ZipArchive;          // create object
        $res = $zip->open($fileName);   // open archive
        if ($res === TRUE) {
          $zip->extractTo('./');        // extract contents to destination directory
          $zip->close();               //close the archieve    
          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 "file" param';
    }
    
    ?>
    

    注意:-更多详情请参考https://www.php.net/manual/en/class.ziparchive.php

    【讨论】:

    • 请解释你的答案,而不是仅仅放一大堆代码。
    【解决方案3】:

    我发现了问题。 代码很好,但托管服务不是,而且他们现在没有可用的 ZIP 扩展名

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多