How to Recursively Zip a Directory in PHP Without Path Pitfalls
Zipping a directory tree in PHP often trips developers over two specific issues: constructing incorrect internal paths within the archive and handling file permissions that differ between operating systems. The code snippet provided in your scenario attempts to walk the directory tree using getDirTree and then iterate through the results. While the logic for iterating looks sound at first glance, there is a critical flaw in how the path strings are constructed before they are handed to the Zip class.
In your current implementation, you are concatenating $target . "/" . $fileLocation. If your target directory is /var/www/public and getDirTree returns relative paths like home/file1.html, your resulting string becomes /var/www/public/home/file1.html. This works for the filesystem check is_file($file), but it creates a problem inside the zip archive itself. Most ZIP parsers, including those on macOS, expect the stored path to be relative to the root of the archive (e.g., home/file1.html), not an absolute or fully qualified system path. If you store absolute paths, extraction tools often fail with "operation not permitted" because they try to write into restricted system directories like /var/www/public/home, which requires elevated privileges.
To fix this, you must strip the target directory prefix from the stored filename before adding it to the zip. You need to ensure that every path added to $zip represents a relative location within the archive structure.
Here is a corrected approach using standard PHP functions to build the path correctly:
$target = '/var/www/html'; // The root of what you want to zip
$aFiles = $this->da->getDirTree($target);
$zip = new Zip();
foreach ($aFiles as $fileLocation => $time) {
// Skip directory entries returned by getDirTree, only process files
if (strpos($fileLocation, '/') === false || !is_file($target . '/' . $fileLocation)) {
continue;
}
$fullPath = $target . '/' . $fileLocation;
// Read the file content
$buffer = file_get_contents($fullPath);
// CRITICAL: Use the relative path, not the absolute one
// This ensures macOS and other systems can extract it correctly
$zip->addFile($buffer, $fileLocation);
}
// Proceed with download logic
THEN_SOME_PHP_CLASS::toDownloadData($zip);
The "operation not permitted" error you are seeing on your Mac is almost certainly a symptom of the home directory being stored as an absolute path (like /var/home/...) or the file permissions preventing extraction to that location. When you unzip via the command line, the terminal often has broader permissions or ignores these checks more leniently than the Finder application.
Regarding the Content-Type header, setting it to application/zip is correct and necessary for modern browsers to recognize the download as a zip file. However, some older clients might still struggle if the MIME type isn't explicitly set in the HTTP response headers before sending the binary data. Ensure your download handler sets the headers correctly:
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="archive.zip"');
THEN_SOME_PHP_CLASS::toDownloadData($zip);
Another common pitfall involves how getDirTree returns data. If it includes directory entries (folders) in the array with a timestamp, your loop might attempt to read them as files or add empty entries to the zip. Always verify that the entry represents an actual file before attempting to read its contents with file_get_contents.
If you find yourself needing more robust handling of paths and permissions, consider leveraging the ZipArchive class built into PHP 5.3+. It offers better control over storing relative paths and setting specific permission bits for the archived files, which can help resolve cross-platform extraction issues without relying on third-party libraries like the one from Seagull Project.