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
  • 182
  • 1
  • 2
  • 11
  • opendir() to be located in `images` and then use $filename only for files.zip – Royal Bg Jul 12 '13 at 09:54
  • I've tested your code. It works as expected. No image folder inside that zip – hek2mgl Jul 12 '13 at 09:55
  • Possible duplicate of this question: [php-creating-zips-without-path-to-files-inside-the-zip](http://stackoverflow.com/questions/3993105/php-creating-zips-without-path-to-files-inside-the-zip?rq=1) –  Jul 12 '13 at 09:55
  • @RoyalBg please give me a code for that, I couldn't – red one Jul 12 '13 at 10:01
  • @hek2mgl yes, but the folder 'images' will be inside that zip too and I don't want that – red one Jul 12 '13 at 10:02
  • @redone Did you read my last comment? – hek2mgl Jul 12 '13 at 10:03
  • Well, there's a whole topic posted here. Maybe better solution than mine. But if you insist I will try to code my solution – Royal Bg Jul 12 '13 at 10:05
  • @RoyalBg that opendir() stuff is bollocks (and not helpful, srry) – hek2mgl Jul 12 '13 at 10:06
  • @redone remove the existing zip from filesystem. I guess if you run the script again the zip will not being created. That's because `zip->open()` will create only a temporary archive. The real work is done by `zip->close()`. If `close()` fails, the temporary zip will be removed, if it succeeds it will be renamed to it's final name. Check the return value of `close()` – hek2mgl Jul 12 '13 at 10:08
  • I removed and tried again but happened the same again. – red one Jul 12 '13 at 10:23
  • @hek2mgl I have 'files.txt' inside 'images' folder, that's why it's happening. – red one Jul 12 '13 at 10:26
  • 1
    Maybe you posted here not exactly the same code that you have? Because (as said @hek2mgl) this code is working fine. – Michael Sivolobov Jul 12 '13 at 10:40
  • one question for you guys.. how can I add multiple files in that zip? – red one Jul 12 '13 at 16:14

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
  • 152,036
  • 28
  • 249
  • 266
  • 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