
- 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 - Traits
- 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 - 除錯
- PHP - 針對 C 開發人員
- PHP - 針對 PERL 開發人員
- PHP - 框架
- PHP - 核心 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 - 檔案是否存在
在對檔案進行任何處理之前,先檢查你嘗試開啟的檔案是否存在通常非常有用。否則,程式可能會引發執行時異常。
PHP 的內建庫在這方面提供了一些實用函式。本章將討論的一些函式包括:
file_exists() - 測試檔案是否存在
is_file() - 如果 fopen() 返回的控制代碼指向檔案或目錄。
is_readable() - 測試你開啟的檔案是否允許讀取資料
is_writable() - 測試是否允許向檔案寫入資料
file_exists() 函式
此函式可用於檔案和目錄。它檢查給定的檔案或目錄是否存在。
file_exists(string $filename): bool
此函式唯一的引數是表示檔案/目錄完整路徑的字串。函式根據檔案是否存在返回 true 或 false。
示例
以下程式檢查檔案“hello.txt”是否存在。
<?php $filename = 'hello.txt'; if (file_exists($filename)) { $message = "The file $filename exists"; } else { $message = "The file $filename does not exist"; } echo $message; ?>
如果檔案存在於當前目錄中,則訊息為:
The file hello.txt exists
如果不存在,則訊息為:
The file hello.txt does not exist
示例
指向檔案的字串可以具有相對路徑或絕對路徑。假設“hello.txt”檔案位於當前目錄內的“hello”子目錄中。
<?php $filename = 'hello/hello.txt'; if (file_exists($filename)) { $message = "The file $filename exists"; } else { $message = "The file $filename does not exist"; } echo $message; ?>
它將產生以下輸出:
The file hello/hello.txt exists
示例
嘗試給出如下所示的絕對路徑:
<?php $filename = 'c:/xampp/htdocs/hello.txt'; if (file_exists($filename)) { $message = "The file $filename exists"; } else { $message = "The file $filename does not exist"; } echo $message; ?>
它將產生以下輸出:
The file c:/xampp/htdocs/hello.txt exists
is_file() 函式
file_exists() 函式對現有檔案和目錄都返回true。is_file() 函式可幫助你確定它是否是檔案。
is_file ( string $filename ) : bool
以下示例顯示了is_file() 函式的工作方式:
<?php $filename = 'hello.txt'; if (is_file($filename)) { $message = "$filename is a file"; } else { $message = "$filename is a not a file"; } echo $message; ?>
輸出表明它是一個檔案:
hello.txt is a file
現在,將“$filename”更改為目錄,然後檢視結果:
<?php $filename = hello; if (is_file($filename)) { $message = "$filename is a file"; } else { $message = "$filename is a not a file"; } echo $message; ?>
現在你會被告知“hello”不是檔案。
請注意,is_file() 函式接受一個$filename,並且僅當$filename 是檔案且存在時才返回true。
is_readable() 函式
有時,你可能想事先檢查是否可以讀取檔案。is_readable() 函式可以確定這一事實。
is_readable ( string $filename ) : bool
示例
以下是is_readable() 函式工作方式的示例:
<?php $filename = 'hello.txt'; if (is_readable($filename)) { $message = "$filename is readable"; } else { $message = "$filename is not readable"; } echo $message; ?>
它將產生以下輸出:
hello.txt is readable
is_writable() 函式
你可以使用 is_writable() 函式來檢查檔案是否存在以及是否可以對給定檔案執行寫入操作。
is_writable ( string $filename ) : bool
示例
以下示例顯示了is_writable() 函式的工作方式:
<?php $filename = 'hello.txt'; if (is_writable($filename)) { $message = "$filename is writable"; } else { $message = "$filename is not writable"; } echo $message; ?>
對於普通的存檔檔案,程式會指出它是可寫的。但是,將其屬性更改為“只讀”並執行程式。現在你得到:
hello.txt is writable