【问题标题】:create zip file inside a folder but without folder inside that zip在文件夹中创建 zip 文件,但在该 zip 中没有文件夹
【发布时间】:2013-07-10 19:59:23
【问题描述】:
$File = "images/files.txt";

$zip = new ZipArchive();
$filename = "./images/files.zip";

if ($zip->open($filename, ZipArchive::CREATE)!==TRUE) {
exit("cannot open <$filename>\n");
}

$zip->addFile("$File");
$zip->close();

此代码在“images”文件夹中创建了一个 files.zip 文件,如果我打开该 zip 文件,“images”文件夹也在那里。我不希望文件夹“图像”在那里。但只有 'files.txt' 文件(位于 images 文件夹内)需要在那里。

文件结构:

  • zip.php

  • 图片

    • files.txt

我该怎么做?

【问题讨论】:

  • opendir() 位于 images 中,然后将 $filename 仅用于 files.zip
  • 我已经测试了你的代码。它按预期工作。该 zip 中没有图像文件夹
  • @RoyalBg 请给我一个代码,我不能
  • 也许您在此处发布的代码与您所拥有的代码不完全相同?因为(如@hek2mgl 所说)这段代码运行良好。

标签: php file zip


【解决方案1】:

@hek2mgl 我在“images”文件夹中有“files.txt”,这就是它发生的原因

那么您的代码将根本无法工作,因为$File 的路径是错误的。使用这个:

$File = "images/files.txt";

$zip = new ZipArchive();
$filename = "./images/files.zip";

if ($zip->open($filename, ZipArchive::CREATE)!==TRUE) {
exit("cannot open <$filename>\n");
}

// use the second parameter of addFile(). It will give the file
// a new name inside the archive. basename() returns the filename
// from a given path
$zip->addFile("$File", basename($File));

if(!$zip->close()) {
    die('failed to create archive');
}

【讨论】:

  • 抱歉,这是我提供给 $File 的目录,但它不起作用
  • @redone 那么现在呢?你测试过 my 的例子吗? 我的示例意味着,从我的答案中复制代码并执行它 - 未更改
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-29
  • 2016-02-06
  • 1970-01-01
  • 2023-03-29
  • 1970-01-01
相关资源
最近更新 更多