PHP如何使用chroot()函数?用法示例

2021年4月2日12:10:18 发表评论 578 次浏览

PHP中的chroot()函数是一个内置函数, 用于将当前进程的根目录更改为directory。 chroot()函数将当前工作目录更改为"/"。 chroot()函数仅对GNU和BSD系统可用, 并且仅在用户使用CLI, CGI或Embed SAPI时可用。除此之外, chroot()函数还需要root特权才能运行。

语法如下:

chroot($directory)

使用的参数:PHP中的chroot()函数仅接受一个参数, 如下所述。

  • $目录:这是必填参数, 用于指定根目录必须更改到的新路径。

返回值:成功返回True, 失败返回False。

错误与异常:

  1. chroot()函数在Windows平台上尚不可用。
  2. 除GNU和BSD外, chroot()函数在SVR4平台上也可用。

以下程序说明了chroot()函数:

程序1:

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

输出如下:

/

程式2:

<?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" );
} 
?>

输出如下:

Root Directory Has Been Successfully Changed

参考: http://php.net/manual/en/function.chroot.php


木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: