What are the potential pitfalls of using chdir() in PHP for file manipulation?

One potential pitfall of using chdir() in PHP for file manipulation is that it can change the current working directory of the script, which may lead to unexpected behavior if not handled carefully. To mitigate this issue, it is recommended to store the current working directory before using chdir() and then restore it afterwards to ensure the script operates as expected.

// Store the current working directory
$oldDir = getcwd();

// Change directory
chdir('/path/to/new/directory');

// Perform file manipulation here

// Restore the old working directory
chdir($oldDir);