What security risks are associated with attempting to copy files to a client's system using PHP?

When copying files to a client's system using PHP, there is a risk of exposing sensitive information or inadvertently allowing malicious files to be uploaded. To mitigate this risk, it is important to validate file types, set proper permissions, and sanitize file names to prevent directory traversal attacks.

// Example PHP code snippet to securely copy files to a client's system

if(isset($_FILES['file'])) {
    $file = $_FILES['file'];
    
    // Validate file type
    $allowedTypes = ['image/jpeg', 'image/png', 'image/gif'];
    if(!in_array($file['type'], $allowedTypes)) {
        die('Invalid file type.');
    }
    
    // Sanitize file name
    $fileName = preg_replace("/[^A-Za-z0-9.]/", '', $file['name']);
    
    // Set proper permissions and move file
    move_uploaded_file($file['tmp_name'], '/path/to/uploaded/files/' . $fileName);
    
    echo 'File uploaded successfully.';
}