檔案的打開和關閉
主要是兩個函式,fopen和fclose,
fopen ( string $filename , string $mode [, bool $use_include_path = FALSE [, resource $context ]] ) : resource
回傳的是資源型別的資料
mode 打開模式:指的是檔案打開是以寫入、讀取、執行等等方式
r:(只讀模式)以只讀方式打開,檔案指標指向檔案的開頭部分
r+:(讀寫模式)以讀寫方式打開,檔案指標指向檔案的開頭
w:(只寫模式)以寫方式打開,指標指向檔案頭部,如果檔案不存在,會創建一個新檔案,如果檔案存在,檔案內容會被清空
w+:(寫讀模式),指標指向檔案頭部,如果檔案不存在,會創建一個新檔案,如果檔案存在,檔案內容會被清空,可以讀取
a:以追加方式打開,指標指向檔案末尾(只寫),如果檔案不存在,會創建一個新檔案
a+:以追加方式打開,如果檔案存在,指標指向檔案末尾,(讀寫)如果檔案不存在,會創建一個新檔案
檔案的讀取
可以讀取單個字符、一行、整個檔案、讀任意長度的資料,
1、讀取整個檔案:
不需要對檔案打開與關閉
- readfile()
readfile ( string $filename [, bool $use_include_path = FALSE [, resource $context ]] ) : int
讀入一個檔案并把它輸出(output)到快取中(buffer) 回傳讀取檔案的位元組數,錯誤則回傳false,
- file()
file ( string $filename [, int $flags = 0 [, resource $context ]] ) : array
讀取整個檔案內容到一個陣列,陣列每個元素對應一行,
- file_get_contents()
file_get_contents ( string $filename [, bool $use_include_path = FALSE [, resource $context [, int $offset = 0 [, int $maxlen ]]]] ) : string
將檔案讀入到一個字串中,從 offset 開始,讀取 maxlen 長度,
該函式適用于二進制物件,是將整個檔案內容讀取到一個字串中的首選方式
It will use memory mapping techniques if supported by your OS to enhance performance.
2、讀取一行:
- fgets() 讀取一行資料
fgets ( resource $handle [, int $length ] ) : string
在讀取指定長度 length 或碰到新行或碰到 EOF 時結束,
- fgetss()
fgetss ( resource $handle [, int $length [, string $allowable_tags ]] ) : string
是fgets()的變體,用于讀取一行資料,不過 會過濾掉被讀取內容的html和php標記,效果同strip_tags()
PHP 7.3.0 中,已被廢棄,不推薦使用,
3、讀取單個字符:
fgetc()
fgetc ( resource $handle ) : string
在對某一個字符進行查找、替換時,需要有針對性的對某個字符進行讀取,那么我們就可以使用fgetc來實作
4、讀取任意長度的字符:
fread()
fread ( resource $handle , int $length ) : string
讀取漢字時,根據漢字的位元組數避免出現亂碼
檔案的寫入
- fwrite()
- fputs()
- file_put_contents(filename,$str)
不需要打開關閉
相當于 fopen("","w\w+")->fwrite()->fclose()
第三個引數(FILE_APPEND)可以實作追加功能
檔案的其他操作
- 復制 copy
copy ( string $source , string $dest [, resource $context ] ) : bool
- 重命名 rename
檔案或檔案夾,可實作剪切功能
rename ( string $oldname , string $newname [, resource $context ] ) : bool
- 洗掉檔案 unlink
unlink ( string $filename [, resource $context ] ) : bool
- 獲取檔案的大小 filesize
filesize ( string $filename ) : int
- 獲取檔案的絕對路徑 realpath
realpath ( string $path ) : string
回傳檔案的絕對路徑(盤符絕對路徑)
- 獲取檔案的相關資訊 stat
stat("filename")
回傳一個陣列,陣列元素是檔案的相關資訊,包括檔案大小、最后的修改時間等 - 判斷是否可寫 is_writeable
- 判斷是否可讀 is_readable
- 判斷是否是個檔案 is_file
- 判斷某個檔案或者目錄是否存在 file_exists
- 檔案最后一次被訪問的時間 fileatime
last access - 檔案最后一次修改的時間 filemtime
modification
檔案指標的操作
PHP可以實作檔案指標的定位和查詢,從而實作所需資訊的查詢,檔案指標有四個函式
rewind feof ftell fseek
- rewind(資源型別):重置指標的位置
- ftell(資源型別):回傳檔案讀寫指標所在的位置,整型值
- feof(資源型別):該函式判斷檔案指標是否指向檔案末尾
- fseek() 位移 距離
fseek ( resource $handle , int $offset [, int $whence = SEEK_SET ] ) : int
whence 可能的值
SEEK_SET - Set position equal to offset bytes.
SEEK_CUR - Set position to current location plus offset.
SEEK_END - Set position to end-of-file plus offset.
檔案鎖 flock
在向一個檔案寫入內容時,需要先將檔案鎖定,以防止其他用戶同時修改該檔案,
flock ( resource $handle , int $operation [, int &$wouldblock ] ) : bool
operation
LOCK_SH: 取得共享鎖
LOCK_EX: 取得獨占鎖 exclusive 排外
LOCK_UN: 釋放鎖定
LOCK_NB: 防止flock()在鎖定的時候堵塞 (防止多人同時鎖定一個檔案)
遠程檔案讀取
在PHP中支持URL格式的檔案的呼叫
在php.ini中開啟 allow_url_fopen = On
//readfile(檔案地址):將檔案內容讀取到記憶體中
資料流(輸出流):記憶體輸出到螢屏的資料流
ob_start(): 開啟輸出流
ob_get_contents():得到輸出流中緩沖的內容
ob_clean:關閉輸出流
可以用來讀取網路上的圖片
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/49438.html
標籤:PHP
下一篇:java運算子
