What are the best practices for handling file uploads in PHP to prevent traffic loss?

When handling file uploads in PHP, it's important to increase the maximum file upload size in your php.ini file to prevent traffic loss when users try to upload large files. Additionally, you should validate file types and sizes on the server side to ensure only allowed files are uploaded. Implementing proper error handling and displaying informative messages to users can also help prevent traffic loss.

// Set maximum file upload size in php.ini
ini_set('upload_max_filesize', '20M');

// Validate file type and size
$allowedTypes = ['image/jpeg', 'image/png'];
$maxSize = 5 * 1024 * 1024; // 5MB

if (in_array($_FILES['file']['type'], $allowedTypes) && $_FILES['file']['size'] <= $maxSize) {
    // Process file upload
} else {
    echo 'Invalid file type or size. Allowed types: JPEG, PNG. Maximum size: 5MB.';
}