Changing the Directory in PHP
If you want to switch to a directory that exists one level up, the chdir function (or cd command on your Operating System) is used. It is quite easy to do it in PHP too. All you need to do is to store the results returned by the dirname() function in a variable and pass it further to the chdir() function.
The chdir() function, a built-in function in PHP, changes the current working directory of the PHP instance to the specified dir_name. It returns TRUE on success or FALSE on failure.
Changing the directory is an important part of handling files in a PHP script. However, there are a few things that you need to keep in mind when using the function.
For example, if you use the function while the filesystem is rooted on a different physical medium than the web server, the function will fail. It is also essential to know the difference between an absolute and a relative path. This is because most functions that handle files will be working with real files and directories located on the disk.
In addition, it is important to remember that in PHP, variables and arrays are passed by value, while objects are passed by reference. This is important to know when dealing with recursive calls to the chdir() function as it will be affected by whether you are passing the argument by value or by reference. Also, if the PHP interpreter is built with ZTS enabled, then any changes made to the current working directory through chdir() will be invisible to the operating system. This means that any other built-in functions will still respect the change in current directory, but external library functions called through FFI will not.