当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


PHP chdir()用法及代码示例


PHP中的chdir()函数用于将PHP当前目录更改为新目录路径。它仅将单个参数用作新目录路径。

用法:

bool chdir(string $new_directory_path)

Parameters Used: This function accepts only one parameter and which is mandatory to be passed.



  • $new_directory_path: This parameter represents the new directory path (i.e. destination path).

返回值: It returns a boolean operator as return value, but actually changes the current directory as desired.

例子:

Input:CWD:/home/
        chdir("gfg")
Output:CWD:/home/gfg
Explanation:Current working directory(CWD)
was changed from '/home/' to '/home/gfg'.


Input:CWD:/home/Documents/
        chdir("foldergfg/inside_folder_gfg")
Output:CWD:/home/Documents/foldergfg/inside_folder_gfg

Explanation:Current working directory (CWD)
was changed from '/home/Documents/' to '/home/
Documents/folder_gfg/inside_folder_gfg'.

错误和异常:
成功时返回TRUE,失败时返回FALSE。因此,它在失败时给出错误/E_WARNING。通常,当目标目录路径无效时,会发生故障情况。

适用版本:
此函数适用于PHP 4,PHP 5,PHP 7。

程序1:

<?php 
// To get current working directory 
echo getcwd() . "<br>"; 
  
// Change directory function 
chdir("testing_gfg"); 
  
// To get current working directory 
echo getcwd(); 
?> 

输出:

/var/www/html
/var/www/html/testing_gfg

最初,当前的工作目录为“ /var /www /html”。应用chdir()函数后,当前工作目录更改为“ /var /www /html /testing_gfg”目录。同样,chdir()函数可用于更改目录。
程序2:

<?php 
// To get current working directory 
echo getcwd() . "<br>"; 
  
// Change directory function 
chdir("GFG/Geeks"); 
  
// To get current working directory 
echo getcwd(); 
?> 

输出:

/home
/home/GFG/Geeks

参考文献: http://php.net/manual/en/function.chdir.php



相关用法


注:本文由纯净天空筛选整理自Prasad_Kshirsagar大神的英文原创作品 php | chdir() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。