Home » PHP

PHP chroot() function with example

PHP chroot() function: Here, we are going to learn about the PHP chroot() function with its syntax, parameters, returns type, and example.
Submitted by IncludeHelp, on August 11, 2019

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:

    chroot(directory);

Parameter(s):

  • 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:

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

Example: PHP code to change the root director

<?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

root directory is changed...
/home/folder1

Reference: PHP chroot() function







Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.