What is PHP chdir() Function?
chdir() allows you change your current directory to a different one. By default, your current directory is where your current script exists. After the change takes place, your new directory becomes its current directory of your script. “chdir” is short for “Change Directory”.
Syntax:
chdir(directory)
Parameters:
The Function has 1 parameter which is required-
directory (Required): text. Check example 1.
Return Values:
The function returns-
- TRUE – if it changes the directory successfully
- FALSE – on failure. Check example 2. If you specify a wrong directory path, it will generate a warning of “No such file or directory”.
How to use chedir() in coding?
As the function returns a Boolean value TRUE/FALSE, you can check this by conditional statements i.e. if statement, ternary operator. Choose one that suits your requirement. Depending on the result the conditional statement returns, your program flow progresses. see the code snippet below-
<?php
$path = "docs";
if(@chdir($path)){
echo getcwd();
}else{
echo "No such directory exists named $path.";
}
?>
Examples:
Example 1: Changing directory path-
<?php
echo "Current directory is: " . getcwd() . "<br />";
chdir('docs');
echo "Setting 'docs' as path, the new current directory is: " . getcwd() . "<br />";
?>
Output:
Current directory is: D:\xampp\htdocs\php
Setting ‘docs’ as path, the new current directory is: D:\xampp\htdocs\php\docs
Explanation:
Line 2: The getcwd() function displays the full directory path of your current script which is “D:\xampp\htdocs\php”.
Line 3: The chedir(‘docs’) function changes the current directory to one level down to “docs” which changes the current directory to “D:\xampp\htdocs\php\docs”.
Line 4: After changing the directory, the getcwd() function displays the current directory.
Example 2: Changing directory path with non-exist path-
<?php
var_dump(chdir('FolderNotExist'));
?>
Output:
Warning: chdir(): No such file or directory (errno 2) in D:\xampp\htdocs\php\chdir.php on line 2
bool(false)
Explanation:
As there is no directory named “FolderNotExist”, so PHP displays the error.
Example 3: Changing directory with absolute and relative paths-
<?php
echo "Current directory is: " . getcwd() . "<br />";
chdir("D:/xampp/htdocs/");
echo "Setting 'D:/xampp/htdocs/' as path, the current directory is: " . getcwd() . "<br />";
chdir("php");
echo "Setting 'php' as path, the current directory is: " . getcwd() . "<br />";
?>
Output:
Current directory is: D:\xampp\htdocs\php\docs
Setting ‘D:/xampp/htdocs/’ as path, the current directory is: D:\xampp\htdocs
Setting ‘php’ as path, the current directory is: D:\xampp\htdocs\php
Explanation:
Line 3: With absolute path, you can change the current directory to that specific directory. Here it is D:\xampp\htdocs.
Line 5: With relative path, you can change the directory (here ‘PHP” is the sub directory of the current directory) in relation to the current directory (which is “D:\xampp\htdocs\”).
Example 4: Changing path up and down of the current directory-
<?php
echo "Current directory is: " . getcwd() . "<br />";
chdir(".");
echo "Setting '.' as path, the current directory is: " . getcwd() . "<br />";
chdir("../");
echo "Setting '../' as path, the current directory is: " . getcwd() . "<br />";
chdir("../../");
echo "Setting '../../' as path, the current directory is: " . getcwd() . "<br />";
?>
Output:
Current directory is: D:\xampp\htdocs\php
Setting ‘.’ as path, the current directory is: D:\xampp\htdocs\php
Setting ‘../’ as path, the current directory is: D:\xampp\htdocs
Setting ‘../../’ as path, the current directory is: D:\
Explanation:
Line 2: getcwd() displays the current directory – D:\xampp\htdocs\php
Line 3: chdir(.) indicates the current directory. So, line 4 displays the current directory as the same as before.
Line 5: The function chdir(“../”) changes the current directory to one level up which becomes D:\xampp\htdocs that line 6 displays.
Line 7: The function chdir(“../../”) changes the current directory to two level up which becomes D:\ that line 8 displays.
Notes on chdir() Function:
- You can mention absolute path (a complete path of a file or directory from the root) or relative path (a path which is relative to the current working directory). Check example 3.
- In the directory path, single dot (.) indicates current directory and double dot (..) indicates parent directory. Check example 4.
- Not to mention that chdir() function just jump from one directory to another one, it doesn’t create a new directory.
PHP Version Support:
PHP 4, PHP 5, PHP 7, PHP 8
Summary: PHP chdir() Function
The chrdir() function which is a PHP built-in directory function is useful when comes to changing current directory. Ten you can perform other operations in the new directory.