How can file paths be properly configured to avoid "failed to open stream" errors in PHP scripts?

When working with file paths in PHP scripts, it's important to ensure that the paths are correctly configured to avoid "failed to open stream" errors. One common mistake is using relative paths instead of absolute paths, which can lead to the script not being able to locate the file. To solve this issue, always use absolute paths or properly configure the relative paths based on the script's location.

// Incorrect relative path causing "failed to open stream" error
$file = 'data.txt';

// Correcting the path by using absolute path or properly configuring relative path
$file = __DIR__ . '/data.txt';