2
$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. I don't want folder 'images' to be there. But only the 'files.txt' file(located inside images folder) needs to be there.

Files structure:

  • zip.php

  • images

    • files.txt

How can I do that?

red one
  • 172
  • 1
  • 2
  • 11

1 Answers1

4

@hek2mgl I have 'files.txt' inside 'images' folder, that's why it's happening

Then your code will not work at all as the path to $File is wrong. 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');
}
hek2mgl
  • 143,113
  • 25
  • 227
  • 253
  • Sorry, that's the directory I'm giving to $File and it's not working – red one Jul 12 '13 at 10:44
  • 1
    @redone so what now? have you tested **my** example? **my** example means, copy the code from my answer and execute it - **unchanged**! – hek2mgl Jul 12 '13 at 10:51