PHP 8.0.0 Beta 4 available for testing


(PHP 4, PHP 5, PHP 7)

chdirChange directory


chdir ( string $directory ) : bool

Changes PHP's current directory to directory.



The new current directory

Return Values

Returns TRUE on success or FALSE on failure.


Throws an error of level E_WARNING on failure.


Example #1 chdir() example


// current directory
echo getcwd() . "\n";


// current directory
echo getcwd() . "\n";


The above example will output something similar to:



Note: When safe mode is enabled, PHP checks whether the directory in which the script is operating has the same UID (owner) as the script that is being executed.


If the PHP interpreter has been built with ZTS (Zend Thread Safety) enabled, any changes to the current directory made through chdir() will be invisible to the operating system. All built-in PHP functions will still respect the change in current directory; but external library functions called using FFI will not. You can tell whether your copy of PHP was built with ZTS enabled using php -i or the built-in constant PHP_ZTS.

See Also

  • getcwd() - Gets the current working directory

add a note add a note

User Contributed Notes 2 notes

php dot duke at qik dot nl
11 years ago
When changing dir's under windows environments:

/* getcwd() gives you back "c:\temp" */

/* getcwd() gives you back "c:\temp\" */

to work around this inconsistency
doing a chdir('.') after the chdir always gives back "c:\temp"
herwin at snt dot utwente dot nl
14 years ago
When using PHP safe mode and trying to change to a dir that is not accessible due to the safe mode restrictions, the function simply fails without generating any kind of error message.

(Tested in PHP 4.3.10-16, Debian Sarge default)
To Top