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
Please Login to comment...