How can PHP be optimized to efficiently handle large file uploads and downloads on a website?

Large file uploads and downloads can be optimized in PHP by adjusting the configuration settings related to file size limits, memory limits, and execution time limits. Additionally, utilizing streaming techniques for handling large files can help reduce memory usage and improve performance.

// Increase file size limits
ini_set('upload_max_filesize', '100M');
ini_set('post_max_size', '100M');

// Increase memory limit
ini_set('memory_limit', '256M');

// Increase execution time limit
ini_set('max_execution_time', 300);

// Enable output buffering
ob_start();

// Stream file for download
$file = 'path/to/large_file.zip';
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="' . basename($file) . '"');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;