Skip to content
Related Articles

Related Articles

PHP | chroot( ) Function

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 11 Jul, 2018

The chroot() function in PHP is an inbuilt function which is used to change the root directory of the current process to directory. The chroot() function changes the current working directory to “/”. The chroot() function is only available to GNU and BSD systems, and only when the user is using the CLI, CGI or Embed SAPI. Apart from this the chroot() function also requires root privileges for functioning.



Parameters Used: The chroot() function in PHP accepts only one parameter as described below.

  • $directory: It is a mandatory parameter which specifies the new path to which the root directory has to be changed.

Return Value: It returns True on success and False on failure.

Errors And Exceptions:

  1. The chroot() function is not available on windows platforms yet.
  2. Apart from GNU and BSD, the chroot() function is also available on SVR4 platforms.

Below programs illustrate the chroot() function:

Program 1:

// Changing root directory
// displaying current directory
echo getcwd();



Program 2:

// Changing root directory
$flag = chroot("path/gfg/chroot/");
if($flag == true) 
   echo("Root Directory Has Been Successfully Changed");
   echo("Root Directory Cannot Be Changed");


Root Directory Has Been Successfully Changed

Reference :

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!