當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。