PHP開發劄記系列(四)-檔案操作

來源:互聯網
上載者:User
PHP開發筆記系列(四)-檔案操作

??? 對於一般的web應用程式,資料會儲存在資料庫表中,但是檔案操作也是必須的,例如檔案的讀取、寫入等,典型的應用情境是考試報名,當系統啟動時,自動讀取“報名須知”的內容到記憶體中,然後再需要時顯示到頁面當中。這次本文《PHP開發筆記系列(四)-檔案操作》將研究一下PHP中的檔案操作。

?

1. 讀取檔案到數組(file)

??? file()函數可以將檔案按行讀取到一個字串數組中,後續程式可以通過遍曆這個字串數組來做處理。

file:file.phpurl:http://localhost:88/file/file.php
?

2. 讀取檔案到字串(file_get_contents)
??? file_get_contents()函數可以將檔案中的所有內容讀取到一個字串變數中,後續程式可以將這個字串中的內容顯示在頁面,或者寫入到新檔案當中。

file:file_get_contents.phpurl:http://localhost:88/file/readfile.php
?


3. 讀取檔案到螢幕(readfile)
??? readfile()函數直接讀取檔案內容並回顯到螢幕,上傳或下載時也會用到這個函數。

file:readfile.phpurl:http://localhost:88/file/readfile.php

?


??? 上面的方法只能全部或按照新行符讀取檔案中的內容,讀到數組或字串或螢幕中,然後如果需要更加靈活的讀取檔案內容或寫檔案內容,就需要用到檔案控制代碼了,類似於C語言當中的檔案讀寫函數。

4. 一次讀取整個檔案內容(fread、filesize)
??? 對於檔案的大小相對比較小的檔案,可以一次將整個檔案的內容讀取到記憶體當中。步驟是,1)fopen開啟新檔案,並指定使用模式,2)使用fread按照指定的長度讀取檔案內容,3)fclose關閉控制代碼。

file:fread.phpurl:http://localhost:88/file/fread.php
?


5. 分次讀取整個檔案的內容(fgets、feof)

??? fgets函數可以分區擷取檔案的中內容,feof可以判斷檔案是否已經讀取到末尾。

file:fgets.phpurl:http://localhost:88/file/fgets.php
?


6. 一次讀取(file_get_contents),一次寫入(file_put_contents)
??? 有時候對於小檔案讀取和寫入,可以方便的使用file_get_contents()和file_put_contents()方法進行寫入,無需建立檔案控制代碼。

file:readOnce_writeOnce.phpurl:http://localhost:88/file/readOnce_writeOnce.php

?


7. 分行讀取,分行寫入(fwrite)
??? 對於大檔案的讀取和寫入,如果一次load到內容,很有可能導致記憶體溢出或web伺服器宕機,因此需要採用逐行讀取逐行寫入。可以使用file()行數將源檔案讀取到一個字串數組中,然後通過fwrite逐行寫入目標檔案,當然,需要使用fopen開啟控制代碼,寫入結束後使用fclose關閉控制代碼。

file:multiRead_multiWrite.phpurl:http://localhost:88/file/multiRead_multiWrite.php
?

8. 其他檔案處理函數

file:other.phpurl:http://localhost:88/file/other.php';       // is_file()返回1 或 0    echo $fileName.' is '.is_file($fileName)?'file':'directory'.'!'.'
'; // is_readable()返回1 或 0 echo $fileName.' readable:'.is_readable($fileName).'
'; // is_writable()返回1 或 0 echo $fileName.' writable:'.is_writable($fileName).'
'; echo $fileName.' created at:'.filemtime($fileName).'
'; echo $fileName.' created at:'.fileatime($fileName).'
'; echo $fileName.' size:'.filesize($fileName).'
';?>

?

?

9. 目錄讀取(opendire、readdir、closedir)

??? 第一種方法,使用opendir、readdir、closedir函數進行目錄的開啟、讀取、關閉,類似於檔案的fopen、fread、fclose,代碼如下:

file:dir.phpurl:http://localhost:88/file/dir.php';        }elseif (is_file($entry))        {            echo '[FILE] '.$entry. '
'; } } closedir($dp);?>

?

??? 第二種方法,採用php提供的物件導向的dir類,通過傳入一個目錄路徑來構造一個dir執行個體,再通過執行個體的read方法進行目錄讀取,代碼如下:

file:dir-class.phpurl:http://localhost:88/file/dir-class.phpread()){        if(is_dir($entry))        {            echo '[DIR] '.$entry. '
'; }elseif (is_file($entry)) { echo '[FILE] '.$entry. '
'; } } closedir($dp);?>
?

10. 檔案拷貝(copy)

file:copy.phpurl:http://localhost:88/file/copy.php

?

?

11. 檔案移動、重新命名(rename)


?

??? 本文地址:http://ryan-d.iteye.com/blog/1543374

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.