douxie5930
2017-05-03 12:54
浏览 59
已采纳

如何上传zip文件并在PHP中显示?

How can I upload a zip file, display the total size of the file, total number of files in the zip file, and display each file name in php code?

<?php
  if ($_FILES) {
      $fileName = $_FILES['zip']['tmp_name'];
        $zip   = zip_open($fileName);
          if ($zip) {
             while ($zip_entry= $zip_read($zip)) {
               echo "<p>";
               echo "<h3> Name:". zip_entry_name($zip_entry). "<h3>";
                  if (zip_entry_open($zip, $zip_entry)) {
                    echo "<h4>File Contents: </h4> ";
                     $contents = zip_entry_read($zip_entry);
                echo $contents;
                echo "<br /><br/>";
                zip_entry_close($zip_entry);
                  }
             }
              zip_close($zip);
           } 

  }
?>
<form method='post' action='' enctype='multipart/form-data'>
 <input type='file' name='zip'><br/>
 <input type='submit' name='upload' value='upload' />
</form>
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dscc90150010 2017-05-03 13:12
    已采纳

    you can check file size and contain using verious inbuilt php methods . read from here

    Also can check below issue in your code

    remove $ from while($zip_entry= $zip_read($zip)) {

    已采纳该答案
    打赏 评论
  • doutonghang2761 2017-05-03 13:47

    Use ZipArchive()

        <?php
    if ($_FILES) {
        $fileName = $_FILES['zip']['tmp_name'];
        $name = $_FILES['zip']['name'];
        $zip = new ZipArchive();
        if ($zip->open($fileName)) {
            echo "<h3> Name: " . $name . "<h3>";
            echo '<h4>File size: ' . filesize($fileName) . '</h4>';
            echo '<h4>Total files: ' . $zip->numFiles . '</h4>';
            echo "<h4>File Contents: </h4>";
            for ($i = 0; $i < $zip->numFiles; $i++) {
                $stat = $zip->statIndex($i);
                echo basename($stat['name']) . "<br>";
            }
            $zip->close();
        }
    
    }
    
    ?>
    

    Output

    enter image description here

    打赏 评论

相关推荐 更多相似问题