Are there any best practices for handling large image uploads in PHP, especially in low bandwidth situations?

When handling large image uploads in PHP, especially in low bandwidth situations, it is important to optimize the process to prevent timeouts and ensure a smooth user experience. One approach is to increase the maximum execution time and memory limit in PHP settings to accommodate large file sizes. Additionally, you can implement chunked file uploads to break down the image into smaller parts and upload them sequentially.

<?php
// Increase maximum execution time and memory limit
ini_set('max_execution_time', 300); // 5 minutes
ini_set('memory_limit', '256M'); // 256 MB

// Handle chunked file uploads
$chunkSize = 1 * 1024 * 1024; // 1 MB
$targetDir = 'uploads/';

if (!empty($_FILES['file']['tmp_name'])) {
    $fileName = $_POST['name'];
    $chunk = isset($_POST['chunk']) ? $_POST['chunk'] : 0;
    $totalChunks = isset($_POST['chunks']) ? $_POST['chunks'] : 0;

    $targetFile = $targetDir . $fileName;

    move_uploaded_file($_FILES['file']['tmp_name'], $targetFile . '_' . $chunk);

    if ($chunk == $totalChunks - 1) {
        // All chunks uploaded, combine them into a single file
        $outputFile = fopen($targetFile, 'ab');
        for ($i = 0; $i < $totalChunks; $i++) {
            $chunkData = file_get_contents($targetFile . '_' . $i);
            fwrite($outputFile, $chunkData);
            unlink($targetFile . '_' . $i);
        }
        fclose($outputFile);
    }
}
?>