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 "/".

Syntax

The syntax of the chroot() function:

chroot(directory);

Parameters

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.

Note

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 php.net 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...
/home/folder1

When fail:
/
root directory is not changed...

PHP chroot() Function Example

PHP code to change the root directory.

<?php
echo getcwd();

//making a directory
mkdir("/home/folder1");

// 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();
?>

Output

The output of the above example is:

root directory is changed...
/home/folder1

Reference: PHP chroot() function

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


Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.