【问题标题】:php creating zips without path to files inside the zipphp创建没有zip文件路径的zip
【发布时间】:2011-04-28 22:20:43
【问题描述】:

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

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

这是我的代码:

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');

【问题讨论】:

  • 试试-D 开关(大写!)

标签: php zip directory


【解决方案1】:

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

根据the documentation

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

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

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

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

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

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

【讨论】:

  • 奇怪,这个相同的问题在两天内出现了两次...stackoverflow.com/questions/3988496/…
  • LOL 热门问题?奇怪。
  • 感谢 Nathan,5 年后仍然有用。出现此问题的原因是我们都从非常受欢迎的 David Walsh 那里获取了相同的 ZipArchive 脚本:davidwalsh.name/create-zip-php 其中包含该问题 :)
  • 出于某种原因,当我这样做时,我在 ZIP 文件的基础和它们的嵌套目录结构中都获得了文件,这实际上导致所有文件都存在两次并且大小加倍的 ZIP 档案。还有其他人遇到这个问题并找到了解决方案吗?谢谢。
  • @HartleySan,这是因为 ZIP 是之前创建的,并且正在打开以使用没有路径的新文件进行扩展。尝试先删除 ZIP,您将无法同时获得带路径和不带路径的文件。
【解决方案2】:

我认为更好的选择是:

$zip->addFile($file,basename($file));

这只是从路径中提取文件名。

【讨论】:

  • 不知道有内置的 PHP basename 函数
  • 我同意,它更有意义
  • 谢谢兄弟我爱你
【解决方案3】:

这只是我发现对我有用的另一种方法

$zipname = 'file.zip';
$zip = new ZipArchive();
$tmp_file = tempnam('.','');
$zip->open($tmp_file, ZipArchive::CREATE);
$download_file = file_get_contents($file);
$zip->addFromString(basename($file),$download_file);
$zip->close();
header('Content-disposition: attachment; filename='.$zipname);
header('Content-type: application/zip');
readfile($tmp_file);

【讨论】:

    【解决方案4】:

    我用它从 zip 中删除根文件夹

    D:\xampp\htdocs\myapp\assets\index.php
    

    将在 zip 中:

    assets\index.php
    

    我们的代码:

    echo $main_path = str_replace("\\", "/", __DIR__ );// get current folder, which call scipt
    $zip_file = 'myapp.zip';
    
    if (file_exists($main_path) && is_dir($main_path))  
    {
        $zip = new ZipArchive();
    
        if (file_exists($zip_file)) {
            unlink($zip_file); // truncate ZIP
        }
        if ($zip->open($zip_file, ZIPARCHIVE::CREATE)!==TRUE) {
            die("cannot open <$zip_file>\n");
        }
    
        $files = 0;
        $paths = array($main_path);
        while (list(, $path) = each($paths))
        {
            foreach (glob($path.'/*') as $p)
            {
                if (is_dir($p)) {
                    $paths[] = $p;
                } else {
                    // special here: we remove root folder ("D:\xampp\htdocs\myapp\") :D
                    $new_filename = str_replace($main_path."/" , "", $p);
                    $zip->addFile($p, $new_filename);
                    $files++;
    
                    echo $p."<br>\n";
                }
            }
        }
    
        echo 'Total files: '.$files;
    
        $zip->close();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-27
      • 2023-03-12
      • 1970-01-01
      • 1970-01-01
      • 2016-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多