【问题标题】:gzip multiple files upload with phpgzip 使用 php 上传多个文件
【发布时间】:2014-04-09 06:57:19
【问题描述】:

我正在寻找一种压缩上传文件的解决方案。我发现了一段代码可以帮助 gzip 单个文件 (here)。如果有多个文件,据我所知,我们首先需要tar这些文件,然后再压缩它们。

我怎么能用 PHP 做到这一点?

【问题讨论】:

  • 您对客户端和服务器代码都有控制权吗?
  • 是的,我可以控制它们。
  • 那么为什么需要将文件捆绑到 tar 文件中呢?我认为这样做不会有任何重大收获,但肯定会显着增加复杂性
  • 我只知道tar 多个文件的压缩大小显着减小。尽管如此,这种方法仍然无法实现多文件压缩。
  • 只有许多内容相似的非常小的文件才会出现“显着减少”。客户端和服务器在 Linux 上吗?

标签: php file gzip tar


【解决方案1】:

在 PHP 5.3+ 中,您可以使用 PharData 类来创建 tar:

// list of file paths to add
$files = glob('/docs/*.pdf');

$tar = new PharData(__DIR__ . '/myfiles.tar');

foreach ($files as $f) {
    $tar->addFile($f, basename($f));
}

如果 tar 不是很大(或者您有很多 RAM),则可以使用以下命令进行压缩:

$tar->compress(Phar::GZ);

对于使用上述方法可能会引发内存限制错误的大文件,一种替代方法是:

// you might want to adjust the time limit
set_time_limit(0);

$source = fopen($pathToTar, 'rb');
$target = __DIR__ . '/myfiles.tar.gz';
$destination = fopen('compress.zlib://' . $target, 'wb');
stream_copy_to_stream($source, $destination);

参考: http://www.binarytides.com/how-to-create-tar-archives-in-php/

【讨论】:

    【解决方案2】:

    这是上传 zip 文件的代码。

    <?php
    
    if(isset($_POST['btnsubmitzip'])){
    
    $file=  $_FILES['txt_zip']['name'];
    
            $image_path = "../images/zip/".$file;
    
             $copy= copy($_FILES['txt_zip']['tmp_name'] ,$image_path);
    
                if($copy)
                {
                 $message="Your Images successfully uploaded";
                }
                else
                {
                  $message="Error in Image Uploading";
                }
    
                $zip = zip_open($image_path);
    
                if ($zip) {
                  while ($zip_entry = zip_read($zip)) {
                    $fp = fopen(DIR_FS_CATALOG."/images/".zip_entry_name($zip_entry), "w");
                    if (zip_entry_open($zip, $zip_entry, "r")) {
                      $buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
                      fwrite($fp,"$buf");
                      zip_entry_close($zip_entry);
                      fclose($fp);
                    }
                  }
                  zip_close($zip);
                }
    }
    
    ?>
    
            <form action="" name="frm" method="post" enctype="multipart/form-data">
              <table>
                  <tr>
                    <td style="padding-top:30px;">Import Images Zip File &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                    <input type="file" name="txt_zip" size="40"/>
                    (size 300 x 300)
                    </td>
                </tr>
              <tr><td style="padding-left:30px " class="smallText"><font color="#990000"><strong><?php echo $message; ?></strong></font></td></tr>
    
              <tr><td style="padding:20px;"><input type="submit" name="btnsubmitzip" value="Submit"></td></tr>
              </table>
            </form>
    

    【讨论】:

    • 我认为这不是 OP 所追求的。
    猜你喜欢
    • 2017-03-04
    • 1970-01-01
    • 1970-01-01
    • 2016-05-31
    • 2016-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多