What are the potential pitfalls of using relative paths when working with file and folder operations in PHP?

When using relative paths in PHP for file and folder operations, one potential pitfall is that the path may not resolve correctly depending on the current working directory of the script. To avoid this issue, it's recommended to use absolute paths instead, which provide a fixed reference point regardless of the script's location.

// Using absolute path instead of relative path
$absolutePath = __DIR__ . '/path/to/file.txt';
$fileContents = file_get_contents($absolutePath);

// Alternatively, you can use the realpath function to resolve the absolute path
$absolutePath = realpath('path/to/file.txt');
$fileContents = file_get_contents($absolutePath);