简体   繁体   English

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

[英]create zip file inside a folder but without folder inside that 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();

This code creates a files.zip file inside 'images' folder, if I open that zip file, 'images' folder is there too. 这段代码在“ images”文件夹中创建了一个files.zip文件,如果我打开该zip文件,“ images”文件夹也在那里。 I don't want folder 'images' to be there. 我不希望文件夹“图像”存在。 But only the 'files.txt' file(located inside images folder) needs to be there. 但是只有“ files.txt”文件(位于images文件夹中)需要存在。

Files structure: 文件结构:

  • zip.php zip.php

  • images 图片

    • files.txt files.txt

How can I do that? 我怎样才能做到这一点?

@hek2mgl I have 'files.txt' inside 'images' folder, that's why it's happening @ hek2mgl我在“图像”文件夹中有“ files.txt”,这就是为什么它发生了的原因

Then your code will not work at all as the path to $File is wrong. 然后,由于$File的路径错误,您的代码将根本无法工作。 Use this: 用这个:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM