11

I've been at this for a while. This actually worked one time, then never again. it simply does not create the zip file. The file does exist.

$zip = new ZipArchive();
$filename = "./test" . time() .".zip";

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

$thisdir = "$_SERVER[DOCUMENT_ROOT]/zip";
$zip->addFile($thisdir . "/trash-icon.png", "/gabage.png");
echo "numfiles: " . $zip->numFiles . "\n";
echo "status:" . $zip->status . "\n";
$zip->close();

If I add something like

$zip->addFromString("testfilephp.txt", "#1 This is a test string added as testfilephp.txt.\n"); 

it creates the zip with the txt file in it.. but a no go for anytype of existing file.

4
  • /trash-icon.png is almost certainly wrong, as it points to the server's root directory. Are you 100% sure the files you are trying to add in fact exist?
    – Pekka
    Mar 24, 2011 at 17:23
  • Are you certain that /trash-icon.png actually exists, remembering that it is in the filesystem root directory
    – Mark Baker
    Mar 24, 2011 at 17:24
  • hah. I guess it was the file. it's working now. Thanks guys
    – Vance
    Mar 24, 2011 at 17:33

3 Answers 3

14

The ZipArchive::addFile() method accepts the path to the file as its first parameter, but not all paths are created equal. addFile() method silently rejects the file and you never know what went wrong. As can be derived from the question, an alternative approach would be:

// $zip->addFile($file);
$content = file_get_contents($file);
$zip->addFromString(pathinfo ( $file, PATHINFO_BASENAME), $content);

In addition to getting the code working, file_get_contents() also generates decent error messages.

4
  • Clever workaround! I could find my error this way. I was also saved from going insane!
    – dsnunez
    Dec 9, 2014 at 14:26
  • Thanks for the workaround. Even if the file is_readable add file failed. Jan 18, 2016 at 10:38
  • This was the answer that resolved the issue for me. Thank you. Nov 5, 2017 at 20:55
  • Can someone explain to me WHY addfile() is failing silently?
    – abaumg
    Mar 16, 2021 at 9:46
2

The ZipArchive::addFile() method accepts the path to the file as its first parameter.


Here, you are using :

$zip->addFile("/trash-icon.png", "/gabage.png");

Which means you are trying to add the /trash-icon.png file to your archive.


Are you sure this file exists ?

Note there is a / at the beginning of that file's path, which indicates it's an absolute path.
Maybe that / should be removed, to use a relative path ?

1
  • Sorry this was a bad example of one of the many iterations I tried. code is updated
    – Vance
    Mar 24, 2011 at 17:29
1

I had similar kind of issue and it was related with the file that I was going to add to the zip archive.

Before adding file to zip archive, it's always better to check if the file exists.

$thisdir = "$_SERVER[DOCUMENT_ROOT]/zip";
if (file_exists($thisdir . "/trash-icon.png")) {
    $zip->addFile($thisdir . "/trash-icon.png", "/gabage.png");
}

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.