Using ZipArchive with PHP 8 and temporary files

The failure is being caused by the fact that the use of the tempnam function actually creates a zero byte file and that is what ZipArchive::CREATE is complaining about.

The solution was to unlink the temporary file created by tempnam before trying to use it. In the example in the question I simply added unlink($temp_file); immediately after $temp_file = tempnam(sys_get_temp_dir(),'AW');.

The first few lines now look like this:

<?php
declare(strict_types=1);
ini_set('display_errors','1');ini_set('display_startup_errors','1');error_reporting(E_ALL);
    
define('BACKUPDIR','E:\Database_Backups\\');
$backupfile = BACKUPDIR . date('Ymd') . '.zip';
$temp_file  = tempnam(sys_get_temp_dir(),'AW');
unlink($temp_file);

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top