
- PHP 教程
- PHP - 首頁
- PHP - 簡介
- PHP - 安裝
- PHP - 歷史
- PHP - 特性
- PHP - 語法
- PHP - Hello World
- PHP - 註釋
- PHP - 變數
- PHP - Echo/Print
- PHP - var_dump
- PHP - $ 和 $$ 變數
- PHP - 常量
- PHP - 魔術常量
- PHP - 資料型別
- PHP - 型別轉換
- PHP - 型別混合
- PHP - 字串
- PHP - 布林值
- PHP - 整數
- PHP - 檔案與 I/O
- PHP - 數學函式
- PHP - Heredoc 和 Nowdoc
- PHP - 複合型別
- PHP - 檔案包含
- PHP - 日期和時間
- PHP - 標量型別宣告
- PHP - 返回型別宣告
- PHP 運算子
- PHP - 運算子
- PHP - 算術運算子
- PHP - 比較運算子
- PHP - 邏輯運算子
- PHP - 賦值運算子
- PHP - 字串運算子
- PHP - 陣列運算子
- PHP - 條件運算子
- PHP - 展開運算子
- PHP - 空值合併運算子
- PHP - 宇宙飛船運算子
- PHP 控制語句
- PHP - 決策
- PHP - If…Else 語句
- PHP - Switch 語句
- PHP - 迴圈型別
- PHP - For 迴圈
- PHP - Foreach 迴圈
- PHP - While 迴圈
- PHP - Do…While 迴圈
- PHP - Break 語句
- PHP - Continue 語句
- PHP 函式
- PHP - 函式
- PHP - 函式引數
- PHP - 按值傳遞
- PHP - 按引用傳遞
- PHP - 預設引數
- PHP - 具名引數
- PHP - 可變引數
- PHP - 返回值
- PHP - 傳遞函式
- PHP - 遞迴函式
- PHP - 型別提示
- PHP - 變數作用域
- PHP - 嚴格型別
- PHP - 匿名函式
- PHP - 箭頭函式
- PHP - 可變函式
- PHP - 區域性變數
- PHP - 全域性變數
- PHP 超全域性變數
- PHP - 超全域性變數
- PHP - $GLOBALS
- PHP - $_SERVER
- PHP - $_REQUEST
- PHP - $_POST
- PHP - $_GET
- PHP - $_FILES
- PHP - $_ENV
- PHP - $_COOKIE
- PHP - $_SESSION
- PHP 檔案處理
- PHP - 檔案處理
- PHP - 開啟檔案
- PHP - 讀取檔案
- PHP - 寫入檔案
- PHP - 檔案是否存在
- PHP - 下載檔案
- PHP - 複製檔案
- PHP - 追加檔案
- PHP - 刪除檔案
- PHP - 處理 CSV 檔案
- PHP - 檔案許可權
- PHP - 建立目錄
- PHP - 列出檔案
- 面向物件 PHP
- PHP - 面向物件程式設計
- PHP - 類和物件
- PHP - 建構函式和解構函式
- PHP - 訪問修飾符
- PHP - 繼承
- PHP - 類常量
- PHP - 抽象類
- PHP - 介面
- PHP - 特性
- PHP - 靜態方法
- PHP - 靜態屬性
- PHP - 名稱空間
- PHP - 物件迭代
- PHP - 封裝
- PHP - Final 關鍵字
- PHP - 過載
- PHP - 克隆物件
- PHP - 匿名類
- PHP Web 開發
- PHP - Web 概念
- PHP - 表單處理
- PHP - 表單驗證
- PHP - 表單郵件/URL
- PHP - 完整表單
- PHP - 檔案包含
- PHP - GET 和 POST
- PHP - 檔案上傳
- PHP - Cookie
- PHP - Session
- PHP - Session 選項
- PHP - 傳送郵件
- PHP - 對輸入進行清理
- PHP - Post-Redirect-Get (PRG)
- PHP - 快閃記憶體訊息
- PHP 高階
- PHP - MySQL
- PHP.INI 檔案配置
- PHP - 陣列解構
- PHP - 編碼規範
- PHP - 正則表示式
- PHP - 錯誤處理
- PHP - Try…Catch
- PHP - Bug 除錯
- PHP - 適用於 C 開發人員
- PHP - 適用於 PERL 開發人員
- PHP - 框架
- PHP - Core PHP 與框架
- PHP - 設計模式
- PHP - 過濾器
- PHP - JSON
- PHP - 異常
- PHP - 特殊型別
- PHP - 雜湊
- PHP - 加密
- PHP - is_null() 函式
- PHP - 系統呼叫
- PHP - HTTP 認證
- PHP - 交換變數
- PHP - Closure::call()
- PHP - 過濾後的 unserialize()
- PHP - IntlChar
- PHP - CSPRNG
- PHP - 期望
- PHP - Use 語句
- PHP - 整數除法
- PHP - 已棄用的功能
- PHP - 已刪除的擴充套件和 SAPI
- PHP - PEAR
- PHP - CSRF
- PHP - FastCGI 程序
- PHP - PDO 擴充套件
- PHP - 內建函式
- PHP 有用資源
- PHP - 速查表
- PHP - 問答
- PHP - 快速指南
- PHP - 線上編譯器
- PHP - 有用資源
- PHP - 討論
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 中的內建目錄函式,用於更改當前目錄。因此,您在新的目錄中可以執行更多操作。