在文件夹中创建zip文件但该zip文件中没有文件夹

时间:2013-07-12 09:51:12

标签: php file zip

$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'文件夹也在那里。我不希望文件夹'images'存在。但只有'files.txt'文件(位于images文件夹中)需要在那里。

文件结构:

  • zip.php

  • 图像

    • files.txt

我该怎么做?

1 个答案:

答案 0 :(得分:4)

  

@ 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');
}
相关问题