php | chdir() Function
The chdir() function in PHP used to change PHP’s current directory to new directory path. It takes only a single argument as new directory path.
Syntax :
bool chdir(string $new_directory_path)
Parameters Used : This function accepts only one parameter and which is mandatory to be passed.
- $new_directory_path : This parameter represents the new directory path (i.e. destination path).
Return Value : It returns a boolean operator as return value, but actually changes the current directory as desired.
Examples :
Input : CWD: /home/
chdir("gfg")
Output : CWD: /home/gfg
Explanation : Current working directory(CWD)
was changed from '/home/' to '/home/gfg'.
Input : CWD: /home/Documents/
chdir("foldergfg/inside_folder_gfg")
Output : CWD: /home/Documents/foldergfg/inside_folder_gfg
Explanation : Current working directory (CWD)
was changed from '/home/Documents/' to '/home/
Documents/folder_gfg/inside_folder_gfg'.
Errors and Exceptions :
This function returns TRUE on success and FALSE on failure. So, it gives an error / E_WARNING on failure. Generally, failure conditions occur when the destination directory path is not valid.
Applicable versions :
This function is applicable in PHP 4, PHP 5, PHP 7.
Program 1:
<?php // To get current working directory echo getcwd() . "<br>"; // Change directory function chdir("testing_gfg"); // To get current working directory echo getcwd(); ?> |
Output :
/var/www/html /var/www/html/testing_gfg
Initially current working directory was ‘/var/www/html’. After applying chdir() function, current working directory changed to ‘/var/www/html/testing_gfg’ directory. Similarly, chdir() function can be used to change directory.
Program 2:
<?php // To get current working directory echo getcwd() . "<br>"; // Change directory function chdir("GFG/Geeks"); // To get current working directory echo getcwd(); ?> |
Output :
/home /home/GFG/Geeks
References : http://php.net/manual/en/function.chdir.php
Recommended Posts:
- How to call a function that return another function in JavaScript ?
- How to get the function name from within that function using JavaScript ?
- How to get the function name inside a function in PHP ?
- D3.js | d3.rgb() Function
- p5.js | hex() function
- PHP | each() Function
- p5.js | str() function
- p5.js | box() Function
- D3.js | d3.hcl() Function
- D3.js | d3.lab() Function
- PHP | Ds\Map last() Function
- PHP | pow( ) Function
- CSS | url() Function
- p5.js | arc() Function
- PHP | Ds\Map map() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



