在講這些函數前,我先給大家說明一下。因為是瞭解函數的常用用法,因此會將某些函數的上下文content參數省略,以方便大家更輕鬆更快的掌握函數用法。我後面也會有對內容相關的講解,敬請期待哦
1> bool mkdir ( string $pathname [, int $mode = 0777 [, bool $recursive = false )
建立目錄
mkdir('f1/b2', 0777, true);//這個屬於目錄嵌套情況,因此$recursive=true
2> bool unlink ( string $filename )
刪除檔案
3> bool copy ( string $source , string $dest )
將source的檔案複製一份給dest檔案,如果路徑沒有將出現警告,如果有相同的檔案名稱將覆蓋
4> resource fopen ( string $filename , string $mode [, bool $use_include_path = false )
開啟檔案
- $filename, 檔案的路徑
- $mode, 開啟的方式
| mode |
解釋 |
| r |
唯讀方式開啟,將檔案指標指向檔案頭。 |
| r+ |
讀寫方式開啟,將檔案指標指向檔案頭。 |
| w |
寫入方式開啟,將檔案指標指向檔案頭並將檔案大小截為零。如果檔案不存在則嘗試建立之。 |
| w+ |
讀寫方式開啟,將檔案指標指向檔案頭並將檔案大小截為零。如果檔案不存在則嘗試建立之。 |
| a |
寫入方式開啟,將檔案指標指向檔案末尾。如果檔案不存在則嘗試建立之。 |
| a+ |
讀寫方式開啟,將檔案指標指向檔案末尾。如果檔案不存在則嘗試建立之。 |
$use_include_path, 表示是否在include_path尋找檔案,true表示尋找
返回一個檔案控制代碼,和opendir函數的開啟目錄類似,返回一個檔案資源
5> bool fclose ( resource $handle )
關閉資源,接受fopen函數的傳回值。
- 對於php檔案流不會主動的被釋放掉,因此需要主動的釋放資源空間。其實其他語言也類似
6> int fwrite ( resource $handle , string $string [, int $length ] )
將$string的內容寫入$handle的資源控制代碼中
$handle = fopen('1.txt', 'w+');$str = '我真聰明';fwrite($handle, $str);
7> string fread ( resource $handle , int $length )
從檔案中讀取內容,length指定讀取的位元組數
8> string fgets ( resource $handle [, int $length ] )
從檔案中讀取一行
$handle = fopen('test5.php', 'r');while($str = fgets($handle)){ echo $str.'
';}fclose($handle);
9> int readfile ( string $filename [, bool $use_include_path = false )
讀取檔案並寫入到輸出緩衝。
/*檔案名稱img.php*/$filename = '1.jpg';header('content-type; image/jpg');//指定下載檔案類型header('content-disposition: attachment; filename="'.$filename.'"');//指定下載檔案的描述,說明是一個附件header('content-length: '.filesize($filename));//指定檔案的大小//將檔案內容讀出來並直接輸出,以便下載readfile($filename);
下載圖片
10> 移動檔案指標
- int ftell ( resource $handle ) 返迴文件當前指標
- int fseek ( resource $handle , int $offset [, int $whence = SEEK_SET ] ) 移動檔案指標到指定的位置
| whence |
解釋 |
| SEEK_CUR |
設定指標位置為當前位置加上第二個參數所提供的offset位移位元組 |
| SEEK_END |
設定指標從檔案末尾的倒數位移量,offset為負值 |
| SEEK_SET |
設定指著東offset開始(預設) |
- bool rewind ( resource $handle ) 移動檔案指標到檔案的開頭