PHP chdir() 函式



PHP 的 chdir() 函式用於選擇指令碼執行的目錄。或者我們可以說 PHP 指令碼自動在安裝位置執行。

但有時,我們可能需要處理位於其他位置的檔案或資料夾。因此,藉助 chdir(),我們可以切換到該位置。這意味著我們可以更改指令碼查詢檔案並執行命令的目錄。

語法

以下是 PHP 的 chdir() 函式的語法:

bool chdir ( string $directory )

引數

以下是 chdir() 函式的必需和可選引數:

序號 引數及描述
1

directory(必需)

新的當前目錄。

返回值

chdir() 函式在成功時返回 TRUE,失敗時返回 FALSE。

PHP 版本

PHP 函式 chdir() 最初是在 PHP 4 核心版本中引入的,並且與 PHP 5、PHP 7、PHP 8 良好相容。

示例

在此 PHP 程式碼中,我們將簡單地使用 PHP 的 chdir() 函式更改當前工作目錄。在 PHP 中,有一個名為 getcwd() 的函式用於獲取當前工作目錄。

所以我們首先會輸出當前工作目錄,然後使用 chdir() 函式更改目錄。

<?php
   // To know the current working directory
   echo getcwd() . "\n";

   // Using chdir() we can change current working directory
   chdir('html');

   // using the command we can get the updated working directory
   echo getcwd() . "\n";
?> 

輸出

這將產生以下結果:

/home/tutorialspoint
/home/tutorialspoint/html

示例

chdir() 函式嘗試將當前工作目錄更改為指定路徑。如果目錄更改成功,chdir() 函式將返回 TRUE,因此 if 塊內的程式碼將被執行。

假設 if 條件失敗,則它將執行 else 部分,其中將列印訊息“無法更改目錄”。請參閱以下程式碼:

<?php
   // Change the current directory to 'new dir'
   if (chdir('home/php/new dir')) {

       echo "Directory changed to 'new dir'.\n";
    
       // List all the files in the new directory 'new dir'
       $files = scandir(getcwd());
       foreach ($files as $file) {
           echo $file . "\n";
       }
   } else {
       echo "Failed to change the directory.";
   }
?> 

輸出

這將生成以下結果。其中“new dir”是我們使用 chdir() 更改的目錄名,index.txt 和 my.txt 是“new dir”資料夾或目錄中的檔案:

Directory changed to 'new dir'.
.
..
index.txt
my.txt

示例

我們將使用 chdir() 函式來解決任何問題並更改當前目錄。如果目錄不存在,我們將使用 if-else 條件處理這種情況。

如果目錄存在,則 if 部分中的程式碼將執行;否則,else 部分中的程式碼將執行。

<?php
    $ourDir = 'unavailable_directory';

    // we will try to change the directory here in if section
    if (chdir($ourDir)) {
        echo "Directory changed to '$ourDir'. \n";
    } else {
        // if directory is not present in the system then print the message
        echo "Failed to change directory to '$ourDir'. Please check if the directory present.";
    }
?> 

輸出

這將產生以下結果:

Failed to change directory to 'unavailable_directory'. Please check if the directory present.

示例

在此示例中,我們將使用 chdir() 函式將目錄導航到當前目錄的上層和下層位置。

使用 chdir(".") 將路徑設定為當前目錄後,它會再次列印當前工作目錄。使用 chdir("../.."),它會輸出當前工作目錄並將其更改為父目錄的父目錄。

<?php
   echo "Initial directory: " . getcwd() . "\n";
   chdir(".");
   echo "After setting path to '.', the current directory is: " . getcwd() . "\n";
   chdir("../");
   echo "After setting path to '../', the current directory is: " . getcwd() . "\n";
   chdir("../../");
   echo "After setting path to '../../', the current directory is: " . getcwd() . "\n";
?> 

輸出

這將產生以下結果:

Initial directory: /Applications/XAMPP/xamppfiles/htdocs/mac
After setting path to '.', the current directory is: /Applications/XAMPP/xamppfiles/htdocs/mac
After setting path to '../', the current directory is: /Applications/XAMPP/xamppfiles/htdocs
After setting path to '../../', the current directory is: /Applications/XAMPP

注意

  • 當前目錄在目錄路徑中用單個點 (.) 表示,父目錄用雙點 (..) 表示。
  • chdir() 函式只是更改目錄,它實際上並沒有建立新目錄。

總結

chdir() 函式是 PHP 中的內建目錄函式,用於更改當前目錄。因此,您在新的目錄中可以執行更多操作。

php_function_reference.htm
廣告