<legend id='SaA0X'><style id='SaA0X'><dir id='SaA0X'><q id='SaA0X'></q></dir></style></legend>
    <bdo id='SaA0X'></bdo><ul id='SaA0X'></ul>
    <tfoot id='SaA0X'></tfoot>
  • <small id='SaA0X'></small><noframes id='SaA0X'>

    <i id='SaA0X'><tr id='SaA0X'><dt id='SaA0X'><q id='SaA0X'><span id='SaA0X'><b id='SaA0X'><form id='SaA0X'><ins id='SaA0X'></ins><ul id='SaA0X'></ul><sub id='SaA0X'></sub></form><legend id='SaA0X'></legend><bdo id='SaA0X'><pre id='SaA0X'><center id='SaA0X'></center></pre></bdo></b><th id='SaA0X'></th></span></q></dt></tr></i><div id='SaA0X'><tfoot id='SaA0X'></tfoot><dl id='SaA0X'><fieldset id='SaA0X'></fieldset></dl></div>

      1. php创建没有zip文件路径的zip文件

        时间:2023-07-15

              <tbody id='vxzIS'></tbody>

            <tfoot id='vxzIS'></tfoot>

              <bdo id='vxzIS'></bdo><ul id='vxzIS'></ul>
                1. <i id='vxzIS'><tr id='vxzIS'><dt id='vxzIS'><q id='vxzIS'><span id='vxzIS'><b id='vxzIS'><form id='vxzIS'><ins id='vxzIS'></ins><ul id='vxzIS'></ul><sub id='vxzIS'></sub></form><legend id='vxzIS'></legend><bdo id='vxzIS'><pre id='vxzIS'><center id='vxzIS'></center></pre></bdo></b><th id='vxzIS'></th></span></q></dt></tr></i><div id='vxzIS'><tfoot id='vxzIS'></tfoot><dl id='vxzIS'><fieldset id='vxzIS'></fieldset></dl></div>
                  <legend id='vxzIS'><style id='vxzIS'><dir id='vxzIS'><q id='vxzIS'></q></dir></style></legend>
                2. <small id='vxzIS'></small><noframes id='vxzIS'>

                  本文介绍了php创建没有zip文件路径的zip文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在尝试使用 php 创建一个 zip 文件(它确实这样做了 - 取自此页面 - http://davidwalsh.name/create-zip-php),但是在 zip 文件中包含文件本身的所有文件夹名称.

                  I'm trying to use php to create a zip file (which it does - taken from this page - http://davidwalsh.name/create-zip-php), however inside the zip file are all of the folder names to the file itself.

                  是否可以只将 zip 中的文件减去所有文件夹?

                  Is it possible to just have the file inside the zip minus all the folders?

                  这是我的代码:

                  function create_zip($files = array(), $destination = '', $overwrite = true) {
                  
                      if(file_exists($destination) && !$overwrite) { return false; };
                      $valid_files = array();
                      if(is_array($files)) {
                          foreach($files as $file) { 
                              if(file_exists($file)) { 
                                  $valid_files[] = $file;
                              };
                          };
                      };
                      if(count($valid_files)) { 
                          $zip = new ZipArchive();
                          if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) { 
                              return false;
                          };
                          foreach($valid_files as $file) { 
                              $zip->addFile($file,$file);
                          };
                          $zip->close();
                          return file_exists($destination);
                      } else {
                          return false;
                      };
                  
                  };
                  
                  
                  $files_to_zip = array('/media/138/file_01.jpg','/media/138/file_01.jpg','/media/138/file_01.jpg');
                  
                  $result = create_zip($files_to_zip,'/...full_site_path.../downloads/138/138_files.zip');
                  

                  推荐答案

                  这里的问题是 $zip->addFile 被传递了相同的两个参数.

                  The problem here is that $zip->addFile is being passed the same two parameters.

                  根据文档:

                  bool ZipArchive::addFile ( string $filename [, string $localname ] )

                  bool ZipArchive::addFile ( string $filename [, string $localname ] )

                  文件名
                  要添加的文件的路径.

                  filename
                  The path to the file to add.

                  本地名称
                  ZIP 存档中的本地名称.

                  localname
                  local name inside ZIP archive.

                  这意味着第一个参数是文件系统中实际文件的路径,第二个是路径 &文件将在存档中的文件名.

                  This means that the first parameter is the path to the actual file in the filesystem and the second is the path & filename that the file will have in the archive.

                  当您提供第二个参数时,您需要在将其添加到 zip 存档时从中删除路径.例如,在基于 Unix 的系统上,这看起来像:

                  When you supply the second parameter, you'll want to strip the path from it when adding it to the zip archive. For example, on Unix-based systems this would look like:

                  $new_filename = substr($file,strrpos($file,'/') + 1);
                  $zip->addFile($file,$new_filename);
                  

                  这篇关于php创建没有zip文件路径的zip文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何使用 PHP 检查目录是否为空? 下一篇:目录中每个文件的循环代码

                  相关文章

                  1. <i id='nsAMV'><tr id='nsAMV'><dt id='nsAMV'><q id='nsAMV'><span id='nsAMV'><b id='nsAMV'><form id='nsAMV'><ins id='nsAMV'></ins><ul id='nsAMV'></ul><sub id='nsAMV'></sub></form><legend id='nsAMV'></legend><bdo id='nsAMV'><pre id='nsAMV'><center id='nsAMV'></center></pre></bdo></b><th id='nsAMV'></th></span></q></dt></tr></i><div id='nsAMV'><tfoot id='nsAMV'></tfoot><dl id='nsAMV'><fieldset id='nsAMV'></fieldset></dl></div>
                  2. <small id='nsAMV'></small><noframes id='nsAMV'>

                      <bdo id='nsAMV'></bdo><ul id='nsAMV'></ul>

                    <legend id='nsAMV'><style id='nsAMV'><dir id='nsAMV'><q id='nsAMV'></q></dir></style></legend>
                    1. <tfoot id='nsAMV'></tfoot>