How can multiple PHP files be archived and downloaded together efficiently?

To archive and download multiple PHP files efficiently, you can create a zip file containing all the files and provide a download link for the zip file. This way, users can download all the files at once in a compressed format.

// Define an array of file paths to be archived
$files = array('file1.php', 'file2.php', 'file3.php');

// Create a zip archive
$zip = new ZipArchive;
$zipname = 'files.zip';
if ($zip->open($zipname, ZipArchive::CREATE) === TRUE) {
    foreach ($files as $file) {
        $zip->addFile($file);
    }
    $zip->close();
}

// Provide download link for the zip file
header('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="' . $zipname . '"');
header('Content-Length: ' . filesize($zipname));
readfile($zipname);