Are there alternative methods or functions in PHP that can be used to create and write to files more securely than fopen and fwrite?

When working with file operations in PHP, it's important to prioritize security to prevent vulnerabilities like directory traversal attacks. One way to enhance security when creating and writing to files is by using the file_put_contents() function instead of fopen() and fwrite(). file_put_contents() simplifies the process by handling both file creation and writing in a single function call, reducing the likelihood of security issues.

// Example of using file_put_contents() to securely create and write to a file
$file = 'example.txt';
$data = 'This is some data to write to the file.';

// Write data to file securely
file_put_contents($file, $data, LOCK_EX);