跟我學習php檔案和目錄常用函數-下篇

來源:互聯網
上載者:User

在講這些函數前,我先給大家說明一下。因為是瞭解函數的常用用法,因此會將某些函數的上下文content參數省略,以方便大家更輕鬆更快的掌握函數用法。我後面也會有對內容相關的講解,敬請期待哦

1> bool mkdir ( string $pathname [, int $mode = 0777 [, bool $recursive = false )

建立目錄

  • $pathname, 目錄路徑
  • $mode, 設定許可權, 0777表示最大許可權
  • $recursive, 是否遞迴建立嵌套的目錄

  • 現在建立一個'f1/f2'的目錄,f1和f2目錄都不存在

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的資源控制代碼中

  • $length, 寫入的位元組長度
$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 ) 移動檔案指標到檔案的開頭
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.