How can PHP scripts be optimized to handle file uploads more efficiently, especially for large files?

When handling file uploads in PHP, it's important to optimize the script to efficiently handle large files. One way to do this is by increasing the upload_max_filesize and post_max_size values in the php.ini file to accommodate larger file sizes. Additionally, using functions like move_uploaded_file() instead of file_get_contents() can help improve performance when dealing with file uploads.

<?php
ini_set('upload_max_filesize', '20M');
ini_set('post_max_size', '20M');

if ($_FILES['file']['error'] === UPLOAD_ERR_OK) {
    $tempFile = $_FILES['file']['tmp_name'];
    $targetPath = 'uploads/';
    $targetFile = $targetPath . $_FILES['file']['name'];

    if (move_uploaded_file($tempFile, $targetFile)) {
        echo 'File uploaded successfully!';
    } else {
        echo 'Failed to upload file.';
    }
} else {
    echo 'Error uploading file.';
}
?>