<?php
/**
* Compresses file contents for synchronous execution testing.
*
* @param string $filePath The path to the file to compress.
* @return string The compressed file contents. Returns false on error.
*/
function compactFileContents(string $filePath): string|false {
if (!file_exists($filePath)) {
return false; // File does not exist
}
$fileContents = file_get_contents($filePath);
if ($fileContents === false) {
return false; // Failed to read file
}
// Use gzcompress for synchronous compression
$compressedContents = gzcompress($fileContents, 9); // 9 is the highest compression level
if ($compressedContents === false) {
return false; // Failed to compress
}
return $compressedContents;
}
//Example usage (replace with your file path)
//$filePath = 'test.txt';
//$compressedData = compactFileContents($filePath);
//if ($compressedData !== false) {
// echo "File compressed successfully.\n";
// //Output the compressed data here or save it to a file.
// echo $compressedData;
//} else {
// echo "File compression failed.\n";
//}
?>
Add your comment