How can PHP scripts be optimized to handle file uploads more efficiently and effectively?

To optimize PHP scripts for handling file uploads more efficiently, you can increase the upload_max_filesize and post_max_size values in your php.ini file to allow larger file uploads. Additionally, you can use functions like move_uploaded_file() to handle file uploads securely and efficiently.

<?php
// Set maximum file size for uploads
ini_set('upload_max_filesize', '20M');
ini_set('post_max_size', '25M');

// Handle file upload
if(isset($_FILES['file'])){
    $target_dir = "uploads/";
    $target_file = $target_dir . basename($_FILES["file"]["name"]);
    
    if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
        echo "File uploaded successfully!";
    } else {
        echo "Error uploading file.";
    }
}
?>