PHP | chroot( ) Function

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.

Syntax:

chroot($directory)

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Changing root directory
chroot("/path/gfg/chroot/");
  
// displaying current directory
echo getcwd();
?>

chevron_right


Output:

/

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

Root Directory Has Been Successfully Changed

Reference : http://php.net/manual/en/function.chroot.php



My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

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.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.