What are the best practices for uploading files in Joomla with PHP?

When uploading files in Joomla with PHP, it is important to ensure that the uploaded files are secure and properly handled to prevent any security vulnerabilities. One best practice is to validate the file type and size before allowing the upload to ensure that only allowed file types and sizes are accepted.

// Check if file is uploaded
if(isset($_FILES['file'])) {
    $file = $_FILES['file'];

    // Validate file type
    $allowed_types = array('jpg', 'jpeg', 'png', 'gif');
    $file_ext = pathinfo($file['name'], PATHINFO_EXTENSION);
    if(!in_array($file_ext, $allowed_types)) {
        die('Invalid file type. Only JPG, JPEG, PNG, GIF files are allowed.');
    }

    // Validate file size
    $max_size = 10 * 1024 * 1024; // 10MB
    if($file['size'] > $max_size) {
        die('File size exceeds the limit of 10MB.');
    }

    // Move uploaded file to desired directory
    move_uploaded_file($file['tmp_name'], 'uploads/' . $file['name']);
}