In what scenarios would it be necessary to compress files before uploading them using a PHP script?

When uploading large files, it can be necessary to compress them before uploading to reduce the upload time and bandwidth usage. This is especially important when dealing with images, videos, or other large files that can be significantly reduced in size through compression. PHP provides functions to compress files using formats like ZIP or GZIP before uploading them to a server.

// Compress file before uploading using PHP
$sourceFile = 'path/to/source/file.jpg';
$compressedFile = 'path/to/compressed/file.zip';

$zip = new ZipArchive();
$zip->open($compressedFile, ZipArchive::CREATE);
$zip->addFile($sourceFile);
$zip->close();

// Now you can upload the compressed file to the server