What are the potential risks or vulnerabilities when using fopen(), fputs(), fwrite(), and fclose() functions in PHP for file creation?

When using fopen(), fputs(), fwrite(), and fclose() functions in PHP for file creation, potential risks or vulnerabilities include file permission issues, lack of input validation leading to possible injection attacks, and resource leaks if the file handle is not properly closed. To mitigate these risks, always ensure proper file permissions are set, validate user input before writing to a file, and remember to close the file handle after writing data.

$file = 'example.txt';
$data = 'Hello, World!';

if ($handle = fopen($file, 'w')) {
    if (fwrite($handle, $data) !== false) {
        fclose($handle);
        echo 'Data written to file successfully.';
    } else {
        echo 'Error writing data to file.';
    }
} else {
    echo 'Error opening file for writing.';
}