WordPress can’t use ZipArchive

I successfully created zip file in root folder of WordPress using following code:

$files = array(__DIR__.'/test.jpg', __DIR__.'/screenshot.png');
$zipname="images.zip";
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE) or die(' cant open ');
foreach ($files as $file) {
 if( $zip->addFile($file)){
    echo "<br>$file Added To Zip Sucessfully";
 }else {
    echo "<br>Can not add $file file";
 }

}
$zip->close();


if(file_exists($zipname)){
    echo "<a href="https://wordpress.stackexchange.com/questions/398433/$zipname" download> Download $zipname</a> ";
}else{
    echo "$zipname not Exists!";
}

I give a full path of images using __DIR__ and test each step by printing messages. you can provide a download link if it is not automatically downloaded. I hope it helps.