PHP Directory chroot() Function (With Examples)

In this tutorial, we will learn about the PHP chroot() function with its usage, syntax, parameters, return value, and examples. By IncludeHelp Last updated : December 31, 2023

PHP chroot() function

The full form of chroot is "Change Root", the function chroot() is used to change the root directory, and, also changes the current working directory to "/".


The syntax of the chroot() function:



The parameters of the chroot() function:

  • directory – It defines the new root directory.

Return Value

It returns a Boolean value, "TRUE" – if root directory changes successfully or "FALSE" – if root directory does not change.


The chroot() will not work on Windows PHP installations. As per the reference manual, the function is only available on PHP when using in CLI/CGI/Embedded SAPI.

The chroot() function requires root privileges. Please refer to the official manual before attempting this function PHP chroot() function.

We are not responsible in any way of any damage this function may cause.

The following is a sample output of the program,

When Success:
root directory is changed...

When fail:
root directory is not changed...

PHP chroot() Function Example

PHP code to change the root directory.

echo getcwd();

//making a directory

// Change root directory
$ret_value = chroot("/home/folder1");

if ($ret_value == true) {
    echo "root directory is changed...";
} else {
    echo "root directory is not changed...";

// Get current directory
echo getcwd();


The output of the above example is:

root directory is changed...

Reference: PHP chroot() function

To understand the above example, you should have the basic knowledge of the following PHP topics:

All PHP Programs

Comments and Discussions!

Load comments ↻

Copyright © 2024 All rights reserved.