# PHP文件操作的函數有哪些
PHP作為廣泛應用于Web開發的腳本語言,提供了豐富的文件操作函數。這些函數允許開發者對文件系統進行各種操作,包括文件的創建、讀取、寫入、刪除、移動等。本文將詳細介紹PHP中常用的文件操作函數及其使用方法。
## 目錄操作函數
### 1. `mkdir()` - 創建目錄
`mkdir()`函數用于創建一個新目錄。
```php
bool mkdir ( string $pathname [, int $mode = 0777 [, bool $recursive = false [, resource $context ]]] )
示例:
if (!file_exists('new_directory')) {
mkdir('new_directory');
}
rmdir()
- 刪除目錄rmdir()
函數用于刪除一個空目錄。
bool rmdir ( string $dirname )
示例:
if (file_exists('empty_directory')) {
rmdir('empty_directory');
}
opendir()
- 打開目錄句柄opendir()
函數用于打開一個目錄句柄,可用于后續的目錄遍歷操作。
resource opendir ( string $path )
示例:
$dir = opendir('some_directory');
while (($file = readdir($dir)) !== false) {
echo "filename: $file\n";
}
closedir($dir);
readdir()
- 讀取目錄條目readdir()
函數從目錄句柄中讀取條目。
string readdir ([ resource $dir_handle ] )
closedir()
- 關閉目錄句柄closedir()
函數關閉由opendir()
打開的目錄句柄。
void closedir ( resource $dir_handle )
scandir()
- 列出目錄中的文件和目錄scandir()
函數返回一個數組,包含指定路徑中的文件和目錄。
array scandir ( string $directory [, int $sorting_order = SCANDIR_SORT_ASCENDING [, resource $context ]] )
示例:
$files = scandir('.');
print_r($files);
file_exists()
- 檢查文件或目錄是否存在file_exists()
函數檢查文件或目錄是否存在。
bool file_exists ( string $filename )
示例:
if (file_exists('test.txt')) {
echo "文件存在";
}
is_file()
- 判斷是否是文件is_file()
函數判斷給定路徑是否是一個常規文件。
bool is_file ( string $filename )
is_dir()
- 判斷是否是目錄is_dir()
函數判斷給定路徑是否是一個目錄。
bool is_dir ( string $filename )
filesize()
- 獲取文件大小filesize()
函數返回文件的大小,以字節為單位。
int filesize ( string $filename )
示例:
$size = filesize('large_file.zip');
echo "文件大小: $size 字節";
filemtime()
- 獲取文件修改時間filemtime()
函數返回文件內容上次被修改的時間戳。
int filemtime ( string $filename )
fileperms()
- 獲取文件權限fileperms()
函數返回文件的權限。
int fileperms ( string $filename )
pathinfo()
- 返回文件路徑信息pathinfo()
函數返回一個關聯數組,包含文件路徑的各個部分。
mixed pathinfo ( string $path [, int $options = PATHINFO_DIRNAME | PATHINFO_BASENAME | PATHINFO_EXTENSION | PATHINFO_FILENAME ] )
示例:
$path_parts = pathinfo('/www/htdocs/index.html');
echo $path_parts['dirname']; // /www/htdocs
echo $path_parts['basename']; // index.html
echo $path_parts['extension']; // html
echo $path_parts['filename']; // index
fopen()
- 打開文件或URLfopen()
函數打開文件或URL。
resource fopen ( string $filename , string $mode [, bool $use_include_path = false [, resource $context ]] )
模式說明: - ‘r’ - 只讀 - ‘w’ - 只寫(會清空文件) - ‘a’ - 追加 - ‘x’ - 創建并只寫 - ‘r+’ - 讀寫 - ‘w+’ - 讀寫(會清空文件) - ‘a+’ - 讀寫(從文件末尾開始) - ‘x+’ - 創建并讀寫
示例:
$handle = fopen('data.txt', 'r');
fclose()
- 關閉文件指針fclose()
函數關閉一個已打開的文件指針。
bool fclose ( resource $handle )
fread()
- 讀取文件fread()
函數讀取打開的文件。
string fread ( resource $handle , int $length )
示例:
$handle = fopen('data.txt', 'r');
$contents = fread($handle, filesize('data.txt'));
fclose($handle);
fwrite()
- 寫入文件fwrite()
函數寫入文件。
int fwrite ( resource $handle , string $string [, int $length ] )
示例:
$handle = fopen('data.txt', 'w');
fwrite($handle, 'Hello World');
fclose($handle);
file_get_contents()
- 將整個文件讀入字符串file_get_contents()
函數將整個文件讀入一個字符串。
string file_get_contents ( string $filename [, bool $use_include_path = false [, resource $context [, int $offset = -1 [, int $maxlen ]]]] )
示例:
$content = file_get_contents('data.txt');
file_put_contents()
- 將字符串寫入文件file_put_contents()
函數將一個字符串寫入文件。
int file_put_contents ( string $filename , mixed $data [, int $flags = 0 [, resource $context ]] )
示例:
file_put_contents('data.txt', 'New content');
file()
- 將文件讀入數組file()
函數將整個文件讀入一個數組。
array file ( string $filename [, int $flags = 0 [, resource $context ]] )
示例:
$lines = file('data.txt');
foreach ($lines as $line) {
echo $line;
}
fgets()
- 從文件指針中讀取一行fgets()
函數從文件指針中讀取一行。
string fgets ( resource $handle [, int $length ] )
示例:
$handle = fopen('data.txt', 'r');
while (($line = fgets($handle)) !== false) {
echo $line;
}
fclose($handle);
feof()
- 測試文件指針是否到達文件末尾feof()
函數測試文件指針是否到達文件末尾。
bool feof ( resource $handle )
ftruncate()
- 將文件截斷到給定的長度ftruncate()
函數將文件截斷到給定的長度。
bool ftruncate ( resource $handle , int $size )
copy()
- 復制文件copy()
函數復制文件。
bool copy ( string $source , string $dest )
示例:
copy('source.txt', 'destination.txt');
rename()
- 重命名或移動文件rename()
函數重命名或移動文件。
bool rename ( string $oldname , string $newname )
示例:
rename('oldname.txt', 'newname.txt');
unlink()
- 刪除文件unlink()
函數刪除文件。
bool unlink ( string $filename )
示例:
unlink('file_to_delete.txt');
realpath()
- 返回規范化的絕對路徑名realpath()
函數返回規范化的絕對路徑名。
string realpath ( string $path )
示例:
echo realpath('../../file.txt');
basename()
- 返回路徑中的文件名部分basename()
函數返回路徑中的文件名部分。
string basename ( string $path [, string $suffix ] )
示例:
echo basename('/www/htdocs/index.html'); // 輸出 'index.html'
dirname()
- 返回路徑中的目錄部分dirname()
函數返回路徑中的目錄部分。
string dirname ( string $path )
示例:
echo dirname('/www/htdocs/index.html'); // 輸出 '/www/htdocs'
flock()
- 便攜式文件鎖定flock()
函數執行簡單的文件鎖定。
bool flock ( resource $handle , int $operation [, int &$wouldblock ] )
鎖定操作: - LOCK_SH - 共享鎖(讀?。?- LOCK_EX - 獨占鎖(寫入) - LOCK_UN - 釋放鎖 - LOCK_NB - 非阻塞鎖
示例:
$fp = fopen('data.txt', 'r+');
if (flock($fp, LOCK_EX)) { // 獲取獨占鎖
fwrite($fp, "寫入數據");
flock($fp, LOCK_UN); // 釋放鎖
} else {
echo "無法獲取鎖!";
}
fclose($fp);
tmpfile()
- 創建臨時文件tmpfile()
函數創建一個臨時文件,文件會在關閉后或腳本結束時自動刪除。
resource tmpfile ( void )
示例:
$temp = tmpfile();
fwrite($temp, "寫入臨時數據");
fclose($temp); // 文件自動刪除
tempnam()
- 創建唯一文件名tempnam()
函數創建一個具有唯一文件名的臨時文件。
string tempnam ( string $dir , string $prefix )
示例:
$temp_file = tempnam('/tmp', 'TMP_');
echo $temp_file;
move_uploaded_file()
- 移動上傳的文件move_uploaded_file()
函數將上傳的文件移動到新位置。
bool move_uploaded_file ( string $filename , string $destination )
示例:
if (move_uploaded_file($_FILES['userfile']['tmp_name'], '/uploads/' . $_FILES['userfile']['name'])) {
echo "文件上傳成功";
}
gzopen()
- 打開gzip文件gzopen()
函數打開一個gzip文件。
resource gzopen ( string $filename , string $mode )
gzread()
- 讀取gzip文件gzread()
函數從gzip文件指針中讀取數據。
string gzread ( resource $zp , int $length )
gzwrite()
- 寫入gzip文件gzwrite()
函數寫入gzip文件。
int gzwrite ( resource $zp , string $string [, int $length ] )
PHP提供了豐富的文件操作函數,涵蓋了從基本的文件讀寫到目錄操作、文件系統管理等多個方面。掌握這些函數對于PHP開發者來說至關重要,能夠幫助開發者高效地處理各種文件操作需求。在實際開發中,應根據具體需求選擇合適的函數,并注意文件操作的權限和安全問題。
通過合理使用這些函數,開發者可以輕松實現文件上傳、日志記錄、配置文件讀取、數據存儲等常見功能,為Web應用開發提供強大的文件處理能力。 “`
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。