What potential security risks are associated with using the $_FILES superglobal in PHP for file uploads?

Using the $_FILES superglobal for file uploads in PHP can pose security risks such as allowing malicious files to be uploaded to the server, leading to potential attacks like file inclusion or code execution. To mitigate these risks, it is important to validate the uploaded file before moving it to a permanent location on the server. This can be done by checking the file type, size, and ensuring it is not executable.

// Example of validating and moving an uploaded file
if(isset($_FILES['file'])) {
    $file = $_FILES['file'];

    // Validate file type
    $allowedTypes = ['image/jpeg', 'image/png'];
    if(!in_array($file['type'], $allowedTypes)) {
        die('Invalid file type');
    }

    // Validate file size
    if($file['size'] > 1000000) {
        die('File size is too large');
    }

    // Move the file to a permanent location
    $uploadPath = '/path/to/uploads/' . $file['name'];
    move_uploaded_file($file['tmp_name'], $uploadPath);
}