What are some potential risks or security concerns when using PHP to create .zip files?

One potential risk when using PHP to create .zip files is the possibility of ZIP file traversal attacks, where an attacker could manipulate the file paths within the ZIP archive to extract files to unintended locations on the server. To mitigate this risk, it is important to sanitize and validate the file paths before adding them to the ZIP archive.

$zip = new ZipArchive();
$zipFile = 'example.zip';

if ($zip->open($zipFile, ZipArchive::CREATE) === TRUE) {
    $fileToZip = 'path/to/file.txt';
    $zipPath = 'files/file.txt';

    // Sanitize and validate the file path before adding it to the ZIP archive
    if (strpos($zipPath, '..') === false) {
        $zip->addFile($fileToZip, $zipPath);
    } else {
        // Handle invalid file path
    }

    $zip->close();
} else {
    // Handle ZIP archive creation error
}