What are the potential pitfalls of using getcwd() and chdir() in PHP?

When using getcwd() and chdir() in PHP, potential pitfalls include not checking for errors when changing directories, leading to unexpected behavior or security vulnerabilities. To solve this, always check the return values of chdir() for errors and handle them appropriately.

$dir = '/path/to/directory';

if (chdir($dir)) {
    echo "Changed directory to $dir";
} else {
    echo "Failed to change directory";
}