What are the potential security risks of uploading files from one domain to another using PHP?

When uploading files from one domain to another using PHP, potential security risks include file injection attacks, cross-site scripting (XSS) vulnerabilities, and unauthorized access to sensitive files on the server. To mitigate these risks, it is crucial to validate and sanitize user input, restrict file types and sizes, and store uploaded files in a secure directory with limited permissions.

// Example PHP code snippet for secure file upload from one domain to another
if(isset($_FILES['file'])){
    $targetDir = '/path/to/secure/directory/';
    $targetFile = $targetDir . basename($_FILES['file']['name']);
    
    // Validate file type and size
    $allowedTypes = array('jpg', 'jpeg', 'png', 'pdf');
    $maxFileSize = 10 * 1024 * 1024; // 10MB
    
    if(in_array(pathinfo($targetFile, PATHINFO_EXTENSION), $allowedTypes) && $_FILES['file']['size'] <= $maxFileSize){
        if(move_uploaded_file($_FILES['file']['tmp_name'], $targetFile)){
            echo 'File uploaded successfully.';
        } else {
            echo 'Error uploading file.';
        }
    } else {
        echo 'Invalid file type or size.';
    }
}