The error ZipArchive::close(): Can't open file: Permission denied occurs when PHP lacks permissions to create/modify ZIP files.
PHP cannot create/modify ZIP files because:
$zip = new ZipArchive();
$filename = "/var/www/html/uploads/archive.zip";
sudo chmod -R 775 /var/www/html/uploads/
sudo chown -R www-data:www-data /var/www/html/uploads/ # Apache
sudo chown -R nginx:nginx /var/www/html/uploads/ # Nginx
php --ini | grep "Loaded Configuration File"
sudo nano /etc/php/8.0/apache2/php.ini
Check these settings:
disable_functions - Ensure exec/system not disabledopen_basedir - Ensure ZIP directory is in allowed pathssudo systemctl restart apache2
sudo systemctl restart nginx php8.0-fpm
# SELinux
sestatus
sudo chcon -R -t httpd_sys_rw_content_t /var/www/html/uploads/
# AppArmor (Ubuntu/Debian)
sudo aa-status
sudo aa-complain /usr/sbin/php-fpm
df -h /var
df -i /var
sudo rm -rf /tmp/*
sys_temp_dir = "/var/www/html/uploads/tmp"
upload_tmp_dir = "/var/www/html/uploads/tmp"
# Create and set permissions
mkdir -p /var/www/html/uploads/tmp
sudo chmod -R 777 /var/www/html/uploads/tmp
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$zip = new ZipArchive();
$file = "/var/www/html/uploads/test.zip";
if ($zip->open($file, ZipArchive::CREATE) !== TRUE) {
die("Error: Unable to create ZIP file. Check permissions.");
}
$zip->addFromString("test.txt", "Hello World!");
$zip->close();
?>
| Issue | Fix | Command |
|---|---|---|
| Folder permissions | Set 775 | chmod -R 775 /var/www/html/uploads/ |
| Wrong ownership | Change owner | chown -R www-data:www-data /var/www/html/uploads/ |
| SELinux block | Set context | chcon -R -t httpd_sys_rw_content_t /var/www/html/uploads/ |
| Full disk/inodes | Clear space | df -h; df -i; rm -rf /tmp/* |
| Temp dir issues | Set in php.ini | sys_temp_dir = "/path/to/tmp" |
| PHP restrictions | Modify php.ini | Check disable_functions & open_basedir |
Follow these fixes to resolve ZipArchive permission errors in PHP.